iOS-textView文本换行高度自动适应
来源:互联网 发布:淘宝卖家三天不发货 编辑:程序博客网 时间:2024/05/18 00:07
由于开发中需要需要个类似QQ空间的一个评论功能,首先要做的输入框肯定是会换行的,因为iOS中(TextField)是单行组件,所以只能使用(TextView)这个多行组件了,可是在不做处理的情况下这个组件的文字在换行的时候会顶出输入框
解决思路:
1.通过Block 在TextView代理方法中把文字内容的ContentSize回调
改变根据文字改变TextView的高度
.htypedef void (^ContentSizeBlock)(CGSize contentSize);@interface ReplyInputView : UIView@property (nonatomic,weak)UITextView *textfd;-(void)setContentSizeBlock:(ContentSizeBlock) block;@end
.m#pragma mark - textView delegate-(void)textViewDidChange:(UITextView *)textView{//根据字长度判断是否隐藏占位符Labelself.lblPlaceholder.hidden = (textView.text.length > 0);CGSize contentSize = self.textfd.contentSize;self.sizeBlock(contentSize);}
2.在Vc中实例化我们输入框View的时候使用block调用更新约束的方法
#pragma mark - 初始化textview-(void)initReplyInputView{ReplyInputView *replyInputView =[[ReplyInputView alloc]initWithFrame:CGRectMake(0,KDeviceH-42-64, KDeviceW, 42) andAboveView:self.view];replyInputView.textdelegate = self;[self.view addSubview:replyInputView];self.replyInputView = replyInputView;__weak typeof(self) weakSelf = self;//使用block[replyInputView setContentSizeBlock:^(CGSize contentSize) {//更新约束[weakSelf updateHeight:contentSize];}];}
更新replyView的高度约束
-(void)updateHeight:(CGSize)contentSize{float height = contentSize.height +20;CGRect frame = self.replyInputView.frame;frame.origin.y -= height - frame.size.height; //高度往上拉伸frame.size.height = height;self.replyInputView.frame = frame;}
这样即可实现了
0 0
- iOS-textView文本换行高度自动适应
- iOS-textView文本换行高度自动适应
- IOS TableView的Cell高度自适应,UILabel自动换行适应
- IOS TableView的Cell高度自适应,UILabel自动换行适应
- IOS TableView的Cell高度自适应,UILabel自动换行适应
- ios TableView的Cell高度自适应,UILabel自动换行适应
- IOS TableView的Cell高度自适应,UILabel自动换行适应
- IOS TableView的Cell高度自适应,UILabel自动换行适应
- IOS TableView的Cell高度自适应,UILabel自动换行适应
- IOS TableView的Cell高度自适应,UILabel自动换行适应
- IOS TableView的Cell高度自适应,UILabel自动换行适应
- IOS TableView的Cell高度自适应,UILabel自动换行适应
- IOS TableView的Cell高度自适应,UILabel自动换行适应
- Uilabel自动换行 UItableView适应高度
- [IOS]UILabel自动适应高度
- IOS TableView的Cell高度自适应,UILabel自动换行适应 table里控件位置自适应
- IOS TableView的Cell高度自适应,UILabel自动换行适应(转载)
- UITableView中Lable自动换行,Cell自动适应高度
- android中webview调用js方法,js调用java方法
- Gray Code
- 开源项目Earth
- Data Binding(数据绑定)用户指南
- Java的文件读写操作
- iOS-textView文本换行高度自动适应
- android 常见分辨率(mdpi、hdpi 、xhdpi、xxhdpi )屏幕适配
- RasSetCredentials
- 我跟你萌讲,我真的要开始认真写博客了!
- VBA批量提取文件
- 获取应用签名
- ActiveMQConnectionFactory : Unsupported major.minor version 51.0解决方案
- spring BeanFactory家族之ClassPathXmlApplicationContext
- [美剧赏析] 权力的游戏<Game of Thrones>完全赏析 (23-24)