iOS: Input align to android implement

This commit is contained in:
pengfei.zhou 2021-09-09 13:55:58 +08:00 committed by osborn
parent 58067e217e
commit abc39ed28f

View File

@ -32,7 +32,6 @@
typedef void (^onSubmitEditingBlock)(NSString *text, DoricInputNode *node); typedef void (^onSubmitEditingBlock)(NSString *text, DoricInputNode *node);
@interface DoricSingleLineInput : UITextField @interface DoricSingleLineInput : UITextField
@property(nonatomic, assign) DoricGravity gravity;
@end @end
@implementation DoricSingleLineInput @implementation DoricSingleLineInput
@ -43,26 +42,6 @@ - (void)drawTextInRect:(CGRect)rect {
- (CGSize)sizeThatFits:(CGSize)size { - (CGSize)sizeThatFits:(CGSize)size {
return [super sizeThatFits: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 @end
@interface DoricMultilineInput : UITextView @interface DoricMultilineInput : UITextView
@ -87,9 +66,7 @@ - (instancetype)init {
- (void)layoutSubviews { - (void)layoutSubviews {
[super layoutSubviews]; [super layoutSubviews];
self.placeholderLabel.hidden = self.text.length > 0; self.placeholderLabel.hidden = self.text.length > 0;
if (self.placeholderLabel.hidden) { if (!self.placeholderLabel.hidden) {
return;
}
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;
@ -108,6 +85,28 @@ - (void)layoutSubviews {
} else { } else {
self.placeholderLabel.centerY = (self.height - textContainerInset.top - textContainerInset.bottom) / 2; self.placeholderLabel.centerY = (self.height - textContainerInset.top - textContainerInset.bottom) / 2;
} }
}
if (self.contentSize.height < self.height) {
if ((self.gravity & DoricGravityTop) == DoricGravityTop) {
self.contentInset = UIEdgeInsetsMake(
0,
self.contentInset.left,
self.contentInset.bottom,
self.contentInset.right);
} else if ((self.gravity & DoricGravityBottom) == DoricGravityBottom) {
self.contentInset = UIEdgeInsetsMake(
self.height - self.contentSize.height,
self.contentInset.left,
self.contentInset.bottom,
self.contentInset.right);
} else {
self.contentInset = UIEdgeInsetsMake(
(self.height - self.contentSize.height) / 2,
self.contentInset.left,
self.contentInset.bottom,
self.contentInset.right);
}
}
} }
- (CGSize)sizeThatFits:(CGSize)size { - (CGSize)sizeThatFits:(CGSize)size {
@ -137,9 +136,13 @@ - (instancetype)init {
if (self = [super init]) { if (self = [super init]) {
_multiLineInput = [DoricMultilineInput new]; _multiLineInput = [DoricMultilineInput new];
_multiLineInput.backgroundColor = UIColor.clearColor; _multiLineInput.backgroundColor = UIColor.clearColor;
_multiLineInput.textAlignment = NSTextAlignmentLeft;
_multiLineInput.gravity = DoricGravityTop;
[self addSubview:_multiLineInput]; [self addSubview:_multiLineInput];
_singleLineInput = [DoricSingleLineInput new]; _singleLineInput = [DoricSingleLineInput new];
_singleLineInput.backgroundColor = UIColor.clearColor; _singleLineInput.backgroundColor = UIColor.clearColor;
_singleLineInput.textAlignment = NSTextAlignmentLeft;
_singleLineInput.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
[self addSubview:_singleLineInput]; [self addSubview:_singleLineInput];
self.multiline = YES; self.multiline = YES;
} }
@ -160,8 +163,14 @@ - (BOOL)multiline {
- (void)setFrame:(CGRect)frame { - (void)setFrame:(CGRect)frame {
[super setFrame:frame]; [super setFrame:frame];
self.singleLineInput.frame = frame; self.singleLineInput.width = frame.size.width - self.doricLayout.paddingLeft - self.doricLayout.paddingRight;
self.multiLineInput.frame = frame; self.singleLineInput.height = frame.size.height - self.doricLayout.paddingTop - self.doricLayout.paddingBottom;
self.singleLineInput.x = self.doricLayout.paddingLeft;
self.singleLineInput.y = self.doricLayout.paddingTop;
self.multiLineInput.width = frame.size.width - self.doricLayout.paddingLeft - self.doricLayout.paddingRight;
self.multiLineInput.height = frame.size.height - self.doricLayout.paddingTop - self.doricLayout.paddingBottom;
self.multiLineInput.x = self.doricLayout.paddingLeft;
self.multiLineInput.y = self.doricLayout.paddingTop;
} }
- (CGSize)sizeThatFits:(CGSize)size { - (CGSize)sizeThatFits:(CGSize)size {
@ -307,6 +316,7 @@ - (DoricInputView *)build {
v.doricLayout.paddingBottom = v.multiLineInput.textContainerInset.bottom; v.doricLayout.paddingBottom = v.multiLineInput.textContainerInset.bottom;
v.doricLayout.paddingLeft = v.multiLineInput.textContainerInset.left; v.doricLayout.paddingLeft = v.multiLineInput.textContainerInset.left;
v.doricLayout.paddingRight = v.multiLineInput.textContainerInset.right; v.doricLayout.paddingRight = v.multiLineInput.textContainerInset.right;
v.multiLineInput.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);
return v; return v;
} }
@ -332,7 +342,6 @@ - (void)blendView:(DoricInputView *)view forPropName:(NSString *)name propValue:
} }
view.textAlignment = alignment; view.textAlignment = alignment;
view.multiLineInput.gravity = gravity; view.multiLineInput.gravity = gravity;
view.singleLineInput.gravity = gravity;
UIControlContentVerticalAlignment verticalAlignment = UIControlContentVerticalAlignmentCenter; UIControlContentVerticalAlignment verticalAlignment = UIControlContentVerticalAlignmentCenter;
if ((gravity & DoricGravityTop) == DoricGravityTop) { if ((gravity & DoricGravityTop) == DoricGravityTop) {
verticalAlignment = UIControlContentVerticalAlignmentTop; verticalAlignment = UIControlContentVerticalAlignmentTop;
@ -451,13 +460,6 @@ - (void)blend:(NSDictionary *)props {
- (void)afterBlended:(NSDictionary *)props { - (void)afterBlended:(NSDictionary *)props {
[super afterBlended:props]; [super afterBlended:props];
if (self.view.multiline) { if (self.view.multiline) {
if (self.view.doricLayout.paddingTop != self.view.multiLineInput.textContainerInset.top
|| self.view.doricLayout.paddingLeft != self.view.multiLineInput.textContainerInset.left
|| self.view.doricLayout.paddingBottom != self.view.multiLineInput.textContainerInset.bottom
|| self.view.doricLayout.paddingRight != self.view.multiLineInput.textContainerInset.right) {
self.view.multiLineInput.textContainerInset = UIEdgeInsetsMake(self.view.doricLayout.paddingTop, self.view.doricLayout.paddingLeft, self.view.doricLayout.paddingBottom, self.view.doricLayout.paddingRight);
}
UIFont *font = self.view.multiLineInput.placeholderLabel.font; UIFont *font = self.view.multiLineInput.placeholderLabel.font;
if (font) { if (font) {
self.view.multiLineInput.placeholderLabel.font = [self.view.multiLineInput.placeholderLabel.font fontWithSize:self.view.font.pointSize]; self.view.multiLineInput.placeholderLabel.font = [self.view.multiLineInput.placeholderLabel.font fontWithSize:self.view.font.pointSize];