怎样改变uitextfield改变高度的placerholder的位置

支持自动布局,自动高度变化和palceholder的输入框
招聘信息:
本文是投稿文章,作者:不罗嗦,这是效果,输入框自动弹起弹下。输入框自动伸缩大小,同时上面的tableView也能自动伸缩。输入框.gif关于输入框自动弹起弹下我另开一篇描述下,这里就不说了,这里重点关注输入框的自动伸缩和palceholder的实现。1、继承UITextView这个是用Auto Layout实现的!当然和网上很多教程一样,这个输入框是用UITextView实现的,不然怎么放大缩小?#import&[UIKit/UIKit.h](因识别问题,此处将尖括号改为方括号)
IB_DESIGNABLE
@interface&KTAutoHeightTextView&:&UITextView
//&是否显示圆角边界
@property&(nonatomic,&assign)&IBInspectable&BOOL&showsRoundC
//&placeholder
@property&(nonatomic,&copy)&IBInspectable&NSString&*
@end这里IB_DESIGNABLE宏和IBInspectable配合使用是为了在xib和storyboard里面可以直接设置属性并实时显示变化效果。showsRoundCorner属性是为了能模仿UITextField的效果。-&(void)setShowsRoundCorner:(BOOL)showsRoundCorner
&&&&_showsRoundCorner&=&showsRoundC
&&&&if&(showsRoundCorner)
&&&&&&&&self.layer.masksToBounds&=&YES;
&&&&&&&&self.layer.cornerRadius&=&5.0;
&&&&&&&&self.layer.borderColor&=&[[UIColor&lightGrayColor]&CGColor];
&&&&&&&&self.layer.borderWidth&=&0.5;
&&&&&&&&self.layer.masksToBounds&=&NO;
&&&&&&&&self.layer.cornerRadius&=&0.0;
&&&&&&&&self.layer.borderColor&=&[[UIColor&clearColor]&CGColor];
&&&&&&&&self.layer.borderWidth&=&0.0;
}然后在你在storyboard里面拖一个UITextView,然后设置class为KTAutoHeightTextView,接着你会发现可以设置属性,这是IB_DESIGNABLE宏和IBInspectable的功劳,设置showsRoundCorner为ON,然后storyboard中就会显示有圆角了!760FD1EC-8B46-42BA-8F50-DDBE.png31C1E98A-3C82-4801-AAF4-7EF2AA519F7F.png2、placeholder实现同样实现placeholder的setter,代码很简单。-&(void)setPlaceholder:(NSString&*)placeholder
&&&&_placeholder&=&
&&&&if&(placeholder)
&&&&&&&&if&(!_placeholderLabel)
&&&&&&&&&&&&_placeholderLabel&=&[[UILabel&alloc]&init];
&&&&&&&&&&&&_placeholderLabel.font&=&self.
&&&&&&&&&&&&_placeholderLabel.textAlignment&=&NSTextAlignmentL
&&&&&&&&&&&&UIEdgeInsets&inset&=&self.textContainerI
&&&&&&&&&&&&CGRect&bounds&=&self.
&&&&&&&&&&&&_placeholderLabel.frame&=&CGRectMake(4.0,&inset.top,&bounds.size.width&-&inset.left&-&inset.right,&self.font.lineHeight);
&&&&&&&&&&&&_placeholderLabel.textColor&=&[UIColor&colorWithWhite:0.801&alpha:1.000];
&&&&&&&&&&&&[self&addSubview:_placeholderLabel];
&&&&&&&&&&&&_placeholderLabel.text&=&
&&&&&&&&_placeholderLabel.hidden&=&self.text.length&>&0;
&&&&&&&&if&(_placeholderLabel)
&&&&&&&&&&&&[_placeholderLabel&removeFromSuperview];
&&&&&&&&&&&&_placeholderLabel&=&
}placeholder还需要在编辑的时候显示或者隐藏,那么这里监听text的长度变化就可以了。-&(instancetype)initWithFrame:(CGRect)frame
&&&&if&(self&=&[super&initWithFrame:frame])
&&&&&&&&[self&setup];
&&&&return&
-&(void)awakeFromNib
&&&&[self&setup];
-&(void)dealloc
&&&&[[NSNotificationCenter&defaultCenter]&removeObserver:self];
-&(void)setup
&&&&[[NSNotificationCenter&defaultCenter]&addObserver:self&selector:@selector(handleTextDidChange:)&name:UITextViewTextDidChangeNotification&object:self];
-&(void)handleTextDidChange:(NSNotification&*)notif
&&&&if&(!self.placeholder)
&&&&UITextView&*textView&=&notif.
&&&&self.placeholderLabel.hidden&=&textView.text.length&>&0;
}也很简单,值得注意的是awakeFromNib是考虑到从xib或者storyboard中加载的时候并不会调用initWithFrame而加上的。为了placeholder的font能跟随UITextView本身的font变化,这里有个小细节,重写font的setter-&(void)setFont:(UIFont&*)font
&&&&[super&setFont:font];
&&&&if&(_placeholderLabel)
&&&&&&&&UIEdgeInsets&insets&=&self.textContainerI
&&&&&&&&CGRect&bounds&=&self.
&&&&&&&&_placeholderLabel.frame&=&CGRectMake(4.0,&insets.top,&bounds.size.width&-&insets.left&-&insets.right,&font.lineHeight);
}3、自动高度变化的实现在对textView进行输入的时候,如果换行了,那么你会发现属性contentSize会改变。这里就是利用contentSize的变化进行缩放高度。这里并没有去计算一堆的boundingRectWithSize: options: attributes: context:因为发现这个有时候并不准!-&(void)layoutSubviews
&&&&[super&layoutSubviews];
&&&&self.layoutFinished&=&YES;
-&(void)setContentSize:(CGSize)contentSize
&&&&[super&setContentSize:contentSize];
&&&&//&监听size变化
&&&&if&(self.font)
&&&&&&&&if&(self.layoutFinished)&//&更新约束或者大小
&&&&&&&&&&&&CGFloat&fitHeight&=&[self&sizeThatFits:CGSizeMake(self.bounds.size.width,&CGFLOAT_MAX)].
&&&&&&&&&&&&if&(fabs(fitHeight&-&self.bounds.size.height)&<&self.font.lineHeight&*&0.5)&//&变化量小于一个行距的0.5倍
&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&[self&findHeightConstraint];
&&&&&&&&&&&&&&&&
&&&&&&&&&&&&}
&&&&&&&&&&&&if&(self.heightConstraint)
&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&self.heightConstraint.constant&=&fitH
&&&&&&&&&&&&&&&&[self&layoutIfNeeded];
&&&&&&&&&&&&}
&&&&&&&&&&&&else
&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&CGRect&bounds&=&self.
&&&&&&&&&&&&&&&&bounds.size.height&=&fitH
&&&&&&&&&&&&&&&&self.bounds&=&
&&&&&&&&&&&&}
&&&&&&&&else&//&查找height约束,记录初值
&&&&&&&&&&&&[self&findHeightConstraint];
-&(void)findHeightConstraint
&&&&if&(self.heightConstraint)
&&&&for&(NSLayoutConstraint&*constraint&in&self.constraints)
&&&&&&&&if&(constraint.secondItem&==&nil&&&&constraint.firstAttribute&==&NSLayoutAttributeHeight)
&&&&&&&&&&&&self.heightConstraint&=&
&&&&&&&&&&&&
}代码是在利用sizeThatFits在contentSize变化的时候去计算合适的大小fitHeight,同时利用一定的规则在合适的时候寻找高度约束heightConstraint。当存在heightConstraint的时候修改约束值,不存在的时候直接修改bounds,从而实现在自动布局或者非自动布局情况下都能自动匹配高度。4、如何设置约束如果在自动布局情况下使用KTAutoHeightTextView,应注意:1、显然你应该有Height这个约束;2、在高度方向上你应该用KTAutoHeightTextView的height去驱动其他view的height而不是反过来。第一句easy不解释,第二句见下图,KTAutoHeightTextView设置了5个约束(一个height,还有上下左右),它的superView(明显起见,我把它设成了灰色)并没有设置height约束。那么它的height就会根据KTAutoHeightTextView的height和上,下三个约束来确定。从而达到这样的效果:KTAutoHeightTextView伸缩,superView也伸缩,上面的tableView也伸缩。恩,这是我们想要的效果!完整的项目/tujinqiu/KTAutoHeightTextView
微信扫一扫
订阅每日移动开发及APP推广热点资讯公众号:CocoaChina
您还没有登录!请或
点击量16297点击量10020点击量10019点击量9095点击量7611点击量6907点击量6149点击量6060点击量5022
&2016 Chukong Technologies,Inc.
京公网安备89支持自动布局,自动高度变化和palceholder的输入框 - 推酷
支持自动布局,自动高度变化和palceholder的输入框
本文是投稿文章,作者:
不罗嗦,这是效果,输入框自动弹起弹下。输入框自动伸缩大小,同时上面的tableView也能自动伸缩。
输入框.gif
关于输入框自动弹起弹下我另开一篇描述下,这里就不说了,这里重点关注输入框的自动伸缩和palceholder的实现。
1、继承UITextView
这个是用Auto Layout实现的!当然和网上很多教程一样,这个输入框是用UITextView实现的,不然怎么放大缩小?
#import [UIKit/UIKit.h](因识别问题,此处将尖括号改为方括号)
IB_DESIGNABLE
@interface KTAutoHeightTextView : UITextView
// 是否显示圆角边界
@property (nonatomic, assign) IBInspectable BOOL showsRoundC
// placeholder
@property (nonatomic, copy) IBInspectable NSString *
这里IB_DESIGNABLE宏和IBInspectable配合使用是为了在xib和storyboard里面可以直接设置属性并实时显示变化效果。showsRoundCorner属性是为了能模仿UITextField的效果。
- (void)setShowsRoundCorner:(BOOL)showsRoundCorner
_showsRoundCorner = showsRoundC
if (showsRoundCorner)
self.layer.masksToBounds = YES;
self.layer.cornerRadius = 5.0;
self.layer.borderColor = [[UIColor lightGrayColor] CGColor];
self.layer.borderWidth = 0.5;
self.layer.masksToBounds = NO;
self.layer.cornerRadius = 0.0;
self.layer.borderColor = [[UIColor clearColor] CGColor];
self.layer.borderWidth = 0.0;
然后在你在storyboard里面拖一个UITextView,然后设置class为KTAutoHeightTextView,接着你会发现可以设置属性,这是IB_DESIGNABLE宏和IBInspectable的功劳,设置showsRoundCorner为ON,然后storyboard中就会显示有圆角了!
760FD1EC-8B46-42BA-8F50-DDBE.png
31C1E98A-3C82-4801-AAF4-7EF2AA519F7F.png
2、placeholder实现
同样实现placeholder的setter,代码很简单。
- (void)setPlaceholder:(NSString *)placeholder
_placeholder =
if (placeholder)
if (!_placeholderLabel)
_placeholderLabel = [[UILabel alloc] init];
_placeholderLabel.font = self.
_placeholderLabel.textAlignment = NSTextAlignmentL
UIEdgeInsets inset = self.textContainerI
CGRect bounds = self.
_placeholderLabel.frame = CGRectMake(4.0, inset.top, bounds.size.width - inset.left - inset.right, self.font.lineHeight);
_placeholderLabel.textColor = [UIColor colorWithWhite:0.801 alpha:1.000];
[self addSubview:_placeholderLabel];
_placeholderLabel.text =
_placeholderLabel.hidden = self.text.length & 0;
if (_placeholderLabel)
[_placeholderLabel removeFromSuperview];
_placeholderLabel =
placeholder还需要在编辑的时候显示或者隐藏,那么这里监听text的长度变化就可以了。
- (instancetype)initWithFrame:(CGRect)frame
if (self = [super initWithFrame:frame])
[self setup];
- (void)awakeFromNib
[self setup];
- (void)dealloc
[[NSNotificationCenter defaultCenter] removeObserver:self];
- (void)setup
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextDidChange:) name:UITextViewTextDidChangeNotification object:self];
- (void)handleTextDidChange:(NSNotification *)notif
if (!self.placeholder)
UITextView *textView = notif.
self.placeholderLabel.hidden = textView.text.length & 0;
也很简单,值得注意的是awakeFromNib是考虑到从xib或者storyboard中加载的时候并不会调用initWithFrame而加上的。为了placeholder的font能跟随UITextView本身的font变化,这里有个小细节,重写font的setter
- (void)setFont:(UIFont *)font
[super setFont:font];
if (_placeholderLabel)
UIEdgeInsets insets = self.textContainerI
CGRect bounds = self.
_placeholderLabel.frame = CGRectMake(4.0, insets.top, bounds.size.width - insets.left - insets.right, font.lineHeight);
3、自动高度变化的实现
在对textView进行输入的时候,如果换行了,那么你会发现属性contentSize会改变。这里就是利用contentSize的变化进行缩放高度。这里并没有去计算一堆的boundingRectWithSize: options: attributes: context:因为发现这个有时候并不准!
- (void)layoutSubviews
[super layoutSubviews];
self.layoutFinished = YES;
- (void)setContentSize:(CGSize)contentSize
[super setContentSize:contentSize];
// 监听size变化
if (self.font)
if (self.layoutFinished) // 更新约束或者大小
CGFloat fitHeight = [self sizeThatFits:CGSizeMake(self.bounds.size.width, CGFLOAT_MAX)].
if (fabs(fitHeight - self.bounds.size.height) & self.font.lineHeight * 0.5) // 变化量小于一个行距的0.5倍
[self findHeightConstraint];
if (self.heightConstraint)
self.heightConstraint.constant = fitH
[self layoutIfNeeded];
CGRect bounds = self.
bounds.size.height = fitH
self.bounds =
else // 查找height约束,记录初值
[self findHeightConstraint];
- (void)findHeightConstraint
if (self.heightConstraint)
for (NSLayoutConstraint *constraint in self.constraints)
if (constraint.secondItem == nil && constraint.firstAttribute == NSLayoutAttributeHeight)
self.heightConstraint =
代码是在利用sizeThatFits在contentSize变化的时候去计算合适的大小fitHeight,同时利用一定的规则在合适的时候寻找高度约束heightConstraint。当存在heightConstraint的时候修改约束值,不存在的时候直接修改bounds,从而实现在自动布局或者非自动布局情况下都能自动匹配高度。
4、如何设置约束
如果在自动布局情况下使用KTAutoHeightTextView,应注意:1、显然你应该有Height这个约束;2、在高度方向上你应该用KTAutoHeightTextView的height去驱动其他view的height而不是反过来。第一句easy不解释,第二句见下图,KTAutoHeightTextView设置了5个约束(一个height,还有上下左右),它的superView(明显起见,我把它设成了灰色)并没有设置height约束。那么它的height就会根据KTAutoHeightTextView的height和上,下三个约束来确定。从而达到这样的效果:KTAutoHeightTextView伸缩,superView也伸缩,上面的tableView也伸缩。恩,这是我们想要的效果!
完整的项目/tujinqiu/KTAutoHeightTextView
已发表评论数()
已收藏到推刊!
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
没有分页内容
图片无法显示
视频无法显示
与原文不一致IOS_改变UITextField&placeHolder颜色、字体
我们有时需要定制化UITextField对象的风格,可以添加许多不同的重写方法,来改变文本字段的显示行为。这些方法都会返回一个CGRect结构,制定了文本字段每个部件的边界范围,甚至修改placeHolder颜色,字体。
& textRectForBounds:  &
& //重写来重置文字区域
drawTextInRect:  & &
& &//改变绘文字属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了.
& placeholderRectForBounds:  //重写来重置占位符区域
drawPlaceholderInRect:  //重写改变绘制占位符属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了
& borderRectForBounds:  //重写来重置边缘区域
& editingRectForBounds:  //重写来重置编辑区域
& clearButtonRectForBounds:  //重写来重置clearButton位置,改变size可能导致button的图片失真
& leftViewRectForBounds:
& rightViewRectForBounds:
通过& drawPlaceholderInRect:方法可改变placeHolder颜色、字体,请看代码:
首先定义一个类CustomTextField让它继承UITextField实现以下方法即可:
//控制清除按钮的位置
-(CGRect)clearButtonRectForBounds:(CGRect)bounds
& &return&CGRectMake(bounds.origin.x&+
bounds.size.width&-&50,
bounds.origin.y&+
bounds.size.height&-20,&16,&16);
//控制placeHolder的位置,左右缩20
-(CGRect)placeholderRectForBounds:(CGRect)bounds
& & //return CGRectInset(bounds,
& &CGRect&inset
=&CGRectMake(bounds.origin.x+100, bounds.origin.y, bounds.size.width&-10, bounds.size.height);//更好理解些
& &return&
//控制显示文本的位置
-(CGRect)textRectForBounds:(CGRect)bounds
&&//return
CGRectInset(bounds, 50, 0);
&& &CGRect&inset
=&CGRectMake(bounds.origin.x+190, bounds.origin.y, bounds.size.width&-10, bounds.size.height);//更好理解些
&& &return&
//控制编辑文本的位置
-(CGRect)editingRectForBounds:(CGRect)bounds
&&//return CGRectInset(
bounds, 10 , 0 );
& &CGRect&inset
=&CGRectMake(bounds.origin.x&+10, bounds.origin.y, bounds.size.width&-10, bounds.size.height);
& &return&
//控制左视图位置
- (CGRect)leftViewRectForBounds:(CGRect)bounds
& &CGRect&inset
=&CGRectMake(bounds.origin.x&+10, bounds.origin.y, bounds.size.width-250, bounds.size.height);
& &return&
&&//return
CGRectInset(bounds,50,0);
//控制placeHolder的颜色、字体
- (void)drawPlaceholderInRect:(CGRect)rect
&&//CGContextRef context =
UIGraphicsGetCurrentContext();
&&//CGContextSetFillColorWithColor(context,
[UIColor yellowColor].CGColor);
& [[UIColororangeColor]&setFill];
& [[selfplaceholder]&drawInRect:rectwithFont:[UIFontsystemFontOfSize:20]];
//下面是使用CustomTextField的代码,可放在viewDidLoad等方法中
&&_textField&= [[CustomTextField&alloc]&initWithFrame:CGRectMake(20,&150,&280,&30)];
&&_textField.placeholder&=&@"请输入帐号信息";
&&_textField.borderStyle&=&UITextBorderStyleRoundedRect;
&&_textField.textAlignment&=&UITextAlignmentLeft;
&&_textField.delegate&=&self;
&&_textField.clearButtonMode&=&UITextFieldViewModeWhileEditing;
&&_textField.text&=&@"aa";
&&UIImageView&*imgv =
[[UIImageView&alloc]&initWithImage:[UIImage&imageNamed:@"icon-iwant-2.png"]];
& &&_textField.leftView&=
&&_textField.leftViewMode&=&UITextFieldViewModeAlways;
& [self.view&addSubview:_textField];
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。支持自动布局,自动高度变化和palceholder的输入框 - 简书
下载简书移动应用
写了9255字,被74人关注,获得了58个喜欢
支持自动布局,自动高度变化和palceholder的输入框
不罗嗦,这是效果,输入框自动弹起弹下。输入框自动伸缩大小,同时上面的tableView也能自动伸缩。
输入框.gif
关于输入框自动弹起弹下我另开一篇描述下,这里就不说了,这里重点关注输入框的自动伸缩和palceholder的实现。
1、继承UITextView
这个是用Auto Layout实现的!当然和网上很多教程一样,这个输入框是用UITextView实现的,不然怎么放大缩小?
#import &UIKit/UIKit.h&
IB_DESIGNABLE
@interface KTAutoHeightTextView : UITextView
// 是否显示圆角边界
@property (nonatomic, assign) IBInspectable BOOL showsRoundC
// placeholder
@property (nonatomic, copy) IBInspectable NSString *
这里IB_DESIGNABLE宏和IBInspectable配合使用是为了在xib和storyboard里面可以直接设置属性并实时显示变化效果。showsRoundCorner属性是为了能模仿UITextField的效果。
- (void)setShowsRoundCorner:(BOOL)showsRoundCorner
_showsRoundCorner = showsRoundC
if (showsRoundCorner)
self.layer.masksToBounds = YES;
self.layer.cornerRadius = 5.0;
self.layer.borderColor = [[UIColor lightGrayColor] CGColor];
self.layer.borderWidth = 0.5;
self.layer.masksToBounds = NO;
self.layer.cornerRadius = 0.0;
self.layer.borderColor = [[UIColor clearColor] CGColor];
self.layer.borderWidth = 0.0;
然后在你在storyboard里面拖一个UITextView,然后设置class为KTAutoHeightTextView,接着你会发现可以设置属性,这是IB_DESIGNABLE宏和IBInspectable的功劳,设置showsRoundCorner为ON,然后storyboard中就会显示有圆角了!
760FD1EC-8B46-42BA-8F50-DDBE.png
31C1E98A-3C82-4801-AAF4-7EF2AA519F7F.png
2、placeholder实现
同样实现placeholder的setter,代码很简单。
- (void)setPlaceholder:(NSString *)placeholder
_placeholder =
if (placeholder)
if (!_placeholderLabel)
_placeholderLabel = [[UILabel alloc] init];
_placeholderLabel.font = self.
_placeholderLabel.textAlignment = NSTextAlignmentL
UIEdgeInsets inset = self.textContainerI
CGRect bounds = self.
_placeholderLabel.frame = CGRectMake(4.0, inset.top, bounds.size.width - inset.left - inset.right, self.font.lineHeight);
_placeholderLabel.textColor = [UIColor colorWithWhite:0.801 alpha:1.000];
[self addSubview:_placeholderLabel];
_placeholderLabel.text =
_placeholderLabel.hidden = self.text.length & 0;
if (_placeholderLabel)
[_placeholderLabel removeFromSuperview];
_placeholderLabel =
placeholder还需要在编辑的时候显示或者隐藏,那么这里监听text的长度变化就可以了。
- (instancetype)initWithFrame:(CGRect)frame
if (self = [super initWithFrame:frame])
[self setup];
- (void)awakeFromNib
[self setup];
- (void)dealloc
[[NSNotificationCenter defaultCenter] removeObserver:self];
- (void)setup
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextDidChange:) name:UITextViewTextDidChangeNotification object:self];
- (void)handleTextDidChange:(NSNotification *)notif
if (!self.placeholder)
UITextView *textView = notif.
self.placeholderLabel.hidden = textView.text.length & 0;
也很简单,值得注意的是awakeFromNib是考虑到从xib或者storyboard中加载的时候并不会调用initWithFrame而加上的。为了placeholder的font能跟随UITextView本身的font变化,这里有个小细节,重写font的setter
- (void)setFont:(UIFont *)font
[super setFont:font];
if (_placeholderLabel)
UIEdgeInsets insets = self.textContainerI
CGRect bounds = self.
_placeholderLabel.frame = CGRectMake(4.0, insets.top, bounds.size.width - insets.left - insets.right, font.lineHeight);
3、自动高度变化的实现
在对textView进行输入的时候,如果换行了,那么你会发现属性contentSize会改变。这里就是利用contentSize的变化进行缩放高度。这里并没有去计算一堆的boundingRectWithSize: options: attributes: context:因为发现这个有时候并不准!
- (void)layoutSubviews
[super layoutSubviews];
self.layoutFinished = YES;
- (void)setContentSize:(CGSize)contentSize
[super setContentSize:contentSize];
// 监听size变化
if (self.font)
if (self.layoutFinished) // 更新约束或者大小
CGFloat fitHeight = [self sizeThatFits:CGSizeMake(self.bounds.size.width, CGFLOAT_MAX)].
if (fabs(fitHeight - self.bounds.size.height) & self.font.lineHeight * 0.5) // 变化量小于一个行距的0.5倍
[self findHeightConstraint];
if (self.heightConstraint)
self.heightConstraint.constant = fitH
[self layoutIfNeeded];
CGRect bounds = self.
bounds.size.height = fitH
self.bounds =
else // 查找height约束,记录初值
[self findHeightConstraint];
- (void)findHeightConstraint
if (self.heightConstraint)
for (NSLayoutConstraint *constraint in self.constraints)
if (constraint.secondItem == nil && constraint.firstAttribute == NSLayoutAttributeHeight)
self.heightConstraint =
代码是在利用sizeThatFits在contentSize变化的时候去计算合适的大小fitHeight,同时利用一定的规则在合适的时候寻找高度约束heightConstraint。当存在heightConstraint的时候修改约束值,不存在的时候直接修改bounds,从而实现在自动布局或者非自动布局情况下都能自动匹配高度。
4、如何设置约束
如果在自动布局情况下使用KTAutoHeightTextView,应注意:1、显然你应该有Height这个约束;2、在高度方向上你应该用KTAutoHeightTextView的height去驱动其他view的height而不是反过来。第一句easy不解释,第二句见下图,KTAutoHeightTextView设置了5个约束(一个height,还有上下左右),它的superView(明显起见,我把它设成了灰色)并没有设置height约束。那么它的height就会根据KTAutoHeightTextView的height和上,下三个约束来确定。从而达到这样的效果:KTAutoHeightTextView伸缩,superView也伸缩,上面的tableView也伸缩。恩,这是我们想要的效果!
2452D19F-A480-443E-B0DC-2F591F3477B8.png
完整的项目
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
被以下专题收入,发现更多相似内容:
分享 iOS 开发的知识,解决大家遇到的问题,讨论iOS开发的前沿,欢迎大家投稿~
· 15409人关注
· 3003人关注
收集 iOS && Android 开发的知识,解决大家遇到的问题,讨论移动前端开发的前沿,欢迎大家投稿~
投稿须知:
· 147人关注
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
选择支付方式:}

我要回帖

更多关于 uitextfield文字位置 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信