iOS:TextField support virtical alignment
This commit is contained in:
parent
228c2c131c
commit
58067e217e
@ -36,11 +36,6 @@ - (void)localeChanged {
|
|||||||
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
|
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
|
||||||
// Override point for customization after application launch.
|
// Override point for customization after application launch.
|
||||||
NSArray<NSString *> *fonts = [UIFont familyNames];
|
NSArray<NSString *> *fonts = [UIFont familyNames];
|
||||||
|
|
||||||
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
||||||
selector:@selector(localeChanged)
|
|
||||||
name:UITextFieldTextDidChangeNotification
|
|
||||||
object:nil];
|
|
||||||
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
|
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
|
||||||
self.rootVC = [[ViewController alloc] init];
|
self.rootVC = [[ViewController alloc] init];
|
||||||
|
|
||||||
|
@ -31,8 +31,42 @@
|
|||||||
|
|
||||||
typedef void (^onSubmitEditingBlock)(NSString *text, DoricInputNode *node);
|
typedef void (^onSubmitEditingBlock)(NSString *text, DoricInputNode *node);
|
||||||
|
|
||||||
|
@interface DoricSingleLineInput : UITextField
|
||||||
|
@property(nonatomic, assign) DoricGravity gravity;
|
||||||
|
@end
|
||||||
|
|
||||||
|
@implementation DoricSingleLineInput
|
||||||
|
- (void)drawTextInRect:(CGRect)rect {
|
||||||
|
[super drawTextInRect:rect];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (CGSize)sizeThatFits:(CGSize)size {
|
||||||
|
return [super sizeThatFits:size];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (CGRect)textRectForBounds:(CGRect)bounds {
|
||||||
|
return UIEdgeInsetsInsetRect(
|
||||||
|
bounds,
|
||||||
|
UIEdgeInsetsMake(
|
||||||
|
self.superview.doricLayout.paddingTop,
|
||||||
|
self.superview.doricLayout.paddingLeft,
|
||||||
|
self.superview.doricLayout.paddingBottom,
|
||||||
|
self.superview.doricLayout.paddingRight));
|
||||||
|
}
|
||||||
|
|
||||||
|
- (CGRect)editingRectForBounds:(CGRect)bounds {
|
||||||
|
return UIEdgeInsetsInsetRect(
|
||||||
|
bounds,
|
||||||
|
UIEdgeInsetsMake(
|
||||||
|
self.superview.doricLayout.paddingTop,
|
||||||
|
self.superview.doricLayout.paddingLeft,
|
||||||
|
self.superview.doricLayout.paddingBottom,
|
||||||
|
self.superview.doricLayout.paddingRight));
|
||||||
|
}
|
||||||
|
@end
|
||||||
|
|
||||||
@interface DoricMultilineInput : UITextView
|
@interface DoricMultilineInput : UITextView
|
||||||
|
@property(nonatomic, assign) DoricGravity gravity;
|
||||||
@property(nonatomic, strong) UILabel *placeholderLabel;
|
@property(nonatomic, strong) UILabel *placeholderLabel;
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@ -59,7 +93,6 @@ - (void)layoutSubviews {
|
|||||||
CGFloat lineFragmentPadding = self.textContainer.lineFragmentPadding;
|
CGFloat lineFragmentPadding = self.textContainer.lineFragmentPadding;
|
||||||
UIEdgeInsets textContainerInset = self.textContainerInset;
|
UIEdgeInsets textContainerInset = self.textContainerInset;
|
||||||
self.placeholderLabel.x = lineFragmentPadding + textContainerInset.left;
|
self.placeholderLabel.x = lineFragmentPadding + textContainerInset.left;
|
||||||
self.placeholderLabel.y = textContainerInset.top;
|
|
||||||
|
|
||||||
CGFloat desiredWidth = self.width - lineFragmentPadding * 2 - textContainerInset.left - textContainerInset.right;
|
CGFloat desiredWidth = self.width - lineFragmentPadding * 2 - textContainerInset.left - textContainerInset.right;
|
||||||
CGSize fitSize = [self.placeholderLabel sizeThatFits:CGSizeMake(desiredWidth, 0)];
|
CGSize fitSize = [self.placeholderLabel sizeThatFits:CGSizeMake(desiredWidth, 0)];
|
||||||
@ -68,6 +101,13 @@ - (void)layoutSubviews {
|
|||||||
self.placeholderLabel.width = desiredWidth;
|
self.placeholderLabel.width = desiredWidth;
|
||||||
}
|
}
|
||||||
self.placeholderLabel.height = fitSize.height;
|
self.placeholderLabel.height = fitSize.height;
|
||||||
|
if ((self.gravity & DoricGravityTop) == DoricGravityTop) {
|
||||||
|
self.placeholderLabel.y = textContainerInset.top;
|
||||||
|
} else if ((self.gravity & DoricGravityBottom) == DoricGravityBottom) {
|
||||||
|
self.placeholderLabel.y = self.height - textContainerInset.bottom - fitSize.height;
|
||||||
|
} else {
|
||||||
|
self.placeholderLabel.centerY = (self.height - textContainerInset.top - textContainerInset.bottom) / 2;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (CGSize)sizeThatFits:(CGSize)size {
|
- (CGSize)sizeThatFits:(CGSize)size {
|
||||||
@ -89,7 +129,7 @@ - (void)setTextAlignment:(NSTextAlignment)textAlignment {
|
|||||||
|
|
||||||
@interface DoricInputView ()
|
@interface DoricInputView ()
|
||||||
@property(nonatomic, strong) DoricMultilineInput *multiLineInput;
|
@property(nonatomic, strong) DoricMultilineInput *multiLineInput;
|
||||||
@property(nonatomic, strong) UITextField *singleLineInput;
|
@property(nonatomic, strong) DoricSingleLineInput *singleLineInput;
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@implementation DoricInputView
|
@implementation DoricInputView
|
||||||
@ -98,7 +138,7 @@ - (instancetype)init {
|
|||||||
_multiLineInput = [DoricMultilineInput new];
|
_multiLineInput = [DoricMultilineInput new];
|
||||||
_multiLineInput.backgroundColor = UIColor.clearColor;
|
_multiLineInput.backgroundColor = UIColor.clearColor;
|
||||||
[self addSubview:_multiLineInput];
|
[self addSubview:_multiLineInput];
|
||||||
_singleLineInput = [UITextField new];
|
_singleLineInput = [DoricSingleLineInput new];
|
||||||
_singleLineInput.backgroundColor = UIColor.clearColor;
|
_singleLineInput.backgroundColor = UIColor.clearColor;
|
||||||
[self addSubview:_singleLineInput];
|
[self addSubview:_singleLineInput];
|
||||||
self.multiline = YES;
|
self.multiline = YES;
|
||||||
@ -118,15 +158,10 @@ - (BOOL)multiline {
|
|||||||
return self.singleLineInput.hidden;
|
return self.singleLineInput.hidden;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)layoutSubviews {
|
- (void)setFrame:(CGRect)frame {
|
||||||
[super layoutSubviews];
|
[super setFrame:frame];
|
||||||
if (self.multiline) {
|
self.singleLineInput.frame = frame;
|
||||||
self.multiLineInput.width = self.width;
|
self.multiLineInput.frame = frame;
|
||||||
self.multiLineInput.height = self.height;
|
|
||||||
} else {
|
|
||||||
self.singleLineInput.width = self.width;
|
|
||||||
self.singleLineInput.height = self.height;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (CGSize)sizeThatFits:(CGSize)size {
|
- (CGSize)sizeThatFits:(CGSize)size {
|
||||||
@ -152,6 +187,7 @@ - (NSString *)text {
|
|||||||
|
|
||||||
- (void)setFont:(UIFont *)font {
|
- (void)setFont:(UIFont *)font {
|
||||||
self.multiLineInput.font = font;
|
self.multiLineInput.font = font;
|
||||||
|
self.multiLineInput.placeholderLabel.font = font;
|
||||||
self.singleLineInput.font = font;
|
self.singleLineInput.font = font;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -191,7 +227,13 @@ - (NSTextAlignment)textAlignment {
|
|||||||
|
|
||||||
- (void)setHintText:(NSString *)text {
|
- (void)setHintText:(NSString *)text {
|
||||||
self.multiLineInput.placeholderLabel.text = text;
|
self.multiLineInput.placeholderLabel.text = text;
|
||||||
self.singleLineInput.placeholder = text;
|
if (text) {
|
||||||
|
self.singleLineInput.attributedPlaceholder = [[NSAttributedString alloc]
|
||||||
|
initWithString:self.multiLineInput.placeholderLabel.text
|
||||||
|
attributes:@{
|
||||||
|
NSForegroundColorAttributeName: self.multiLineInput.placeholderLabel.textColor,
|
||||||
|
NSFontAttributeName: self.multiLineInput.placeholderLabel.font}];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSString *)hintText {
|
- (NSString *)hintText {
|
||||||
@ -204,12 +246,24 @@ - (NSString *)hintText {
|
|||||||
|
|
||||||
- (void)setHintTextColor:(UIColor *)color {
|
- (void)setHintTextColor:(UIColor *)color {
|
||||||
self.multiLineInput.placeholderLabel.textColor = color;
|
self.multiLineInput.placeholderLabel.textColor = color;
|
||||||
[self.singleLineInput setValue:color forKeyPath:@"_placeholderLabel.textColor"];
|
if (self.multiLineInput.placeholderLabel.text) {
|
||||||
|
self.singleLineInput.attributedPlaceholder = [[NSAttributedString alloc]
|
||||||
|
initWithString:self.multiLineInput.placeholderLabel.text
|
||||||
|
attributes:@{
|
||||||
|
NSForegroundColorAttributeName: self.multiLineInput.placeholderLabel.textColor,
|
||||||
|
NSFontAttributeName: self.multiLineInput.placeholderLabel.font}];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)setHintFont:(UIFont *)font {
|
- (void)setHintFont:(UIFont *)font {
|
||||||
self.multiLineInput.placeholderLabel.font = font;
|
self.multiLineInput.placeholderLabel.font = font;
|
||||||
[self.singleLineInput setValue:font forKeyPath:@"_placeholderLabel.font"];
|
if (self.multiLineInput.placeholderLabel.text) {
|
||||||
|
self.singleLineInput.attributedPlaceholder = [[NSAttributedString alloc]
|
||||||
|
initWithString:self.multiLineInput.placeholderLabel.text
|
||||||
|
attributes:@{
|
||||||
|
NSForegroundColorAttributeName: self.multiLineInput.placeholderLabel.textColor,
|
||||||
|
NSFontAttributeName: self.multiLineInput.placeholderLabel.font}];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)setKeyboardType:(UIKeyboardType)keyboardType {
|
- (void)setKeyboardType:(UIKeyboardType)keyboardType {
|
||||||
@ -277,6 +331,15 @@ - (void)blendView:(DoricInputView *)view forPropName:(NSString *)name propValue:
|
|||||||
alignment = NSTextAlignmentRight;
|
alignment = NSTextAlignmentRight;
|
||||||
}
|
}
|
||||||
view.textAlignment = alignment;
|
view.textAlignment = alignment;
|
||||||
|
view.multiLineInput.gravity = gravity;
|
||||||
|
view.singleLineInput.gravity = gravity;
|
||||||
|
UIControlContentVerticalAlignment verticalAlignment = UIControlContentVerticalAlignmentCenter;
|
||||||
|
if ((gravity & DoricGravityTop) == DoricGravityTop) {
|
||||||
|
verticalAlignment = UIControlContentVerticalAlignmentTop;
|
||||||
|
} else if ((gravity & DoricGravityBottom) == DoricGravityBottom) {
|
||||||
|
verticalAlignment = UIControlContentVerticalAlignmentBottom;
|
||||||
|
}
|
||||||
|
view.singleLineInput.contentVerticalAlignment = verticalAlignment;
|
||||||
} else if ([name isEqualToString:@"font"]) {
|
} else if ([name isEqualToString:@"font"]) {
|
||||||
NSString *iconfont = prop;
|
NSString *iconfont = prop;
|
||||||
UIFont *font = [UIFont fontWithName:[iconfont stringByReplacingOccurrencesOfString:@".ttf" withString:@""]
|
UIFont *font = [UIFont fontWithName:[iconfont stringByReplacingOccurrencesOfString:@".ttf" withString:@""]
|
||||||
@ -535,6 +598,11 @@ - (NSString *)limitToHansMaxLength:(NSUInteger)maxLen text:(NSString *)text {
|
|||||||
return [text substringWithRange:NSMakeRange(0, subStringRangeLen)];
|
return [text substringWithRange:NSMakeRange(0, subStringRangeLen)];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)textFieldDidBeginEditing:(UITextField *)textField {
|
||||||
|
if (!textField.window.isKeyWindow) {
|
||||||
|
[textField.window makeKeyAndVisible];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
- (void)textFieldDidEndEditing:(UITextField *)textField {
|
- (void)textFieldDidEndEditing:(UITextField *)textField {
|
||||||
if (self.maxLength) {
|
if (self.maxLength) {
|
||||||
|
Reference in New Issue
Block a user