第十二篇、高度自适应的textView
2016-08-24 09:48
190 查看
高度根据输入内容变化输入框,我们在很多的应用上都可以见到,如微信、QQ聊天,QQ空间评论等等,该输入框可以用xib,纯代码编写,但是个人觉得纯代码编写用起来更加方便一些。
1.使用自定义的UIView控件
2.通过改变textView的中的内容改变textView的frame,在改变父类视图的frame
后期扩充的功能:
3.后期需要扩充表情,发送按钮等等
4.最好是给textView设置一个最大的高度和记录起始的高度(方便恢复原状)
5.布局方式换成约束的方式稍微好点
.h
.m
使用示例:
注意:
光标的位置还需要调整一下,不然不居中,要回到原位。
1.使用自定义的UIView控件
2.通过改变textView的中的内容改变textView的frame,在改变父类视图的frame
后期扩充的功能:
3.后期需要扩充表情,发送按钮等等
4.最好是给textView设置一个最大的高度和记录起始的高度(方便恢复原状)
5.布局方式换成约束的方式稍微好点
.h
#import <UIKit/UIKit.h> @interface CQTextView : UIView @property (nonatomic,copy) NSString *placeholder; @property (nonatomic,strong) UIFont *font; @end
.m
#import "CQTextView.h" @interface CQTextView (){ /** 记录初始化时的height,textview */ CGFloat _initHeight; } @property (nonatomic,strong) UITextView *textView; /** placeholder的label */ @property (nonatomic,strong) UILabel *placeholderLabel; @end @implementation CQTextView /** 重写初始化方法 */ - (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { // 记录初始高度 _initHeight = frame.size.height; self.clipsToBounds = NO; // 添加textView self.textView = [[UITextView alloc]initWithFrame:self.bounds]; [self addSubview:self.textView]; self.textView.delegate = (id)self; self.textView.backgroundColor = [UIColor clearColor]; // 添加placeholderLabel self.placeholderLabel = [[UILabel alloc]initWithFrame:CGRectMake(3, 0, frame.size.width - 3, frame.size.height)]; [self addSubview:self.placeholderLabel]; self.placeholderLabel.backgroundColor = [UIColor clearColor]; self.placeholderLabel.textColor = [UIColor lightGrayColor]; } return self; } // 赋值placeholder - (void)setPlaceholder:(NSString *)placeholder{ _placeholder = placeholder; self.placeholderLabel.text = placeholder; [self.placeholderLabel sizeToFit]; self.placeholderLabel.center = self.textView.center; } // 赋值font - (void)setFont:(UIFont *)font{ self.textView.font = self.placeholderLabel.font = font; // 重新调整placeholderLabel的大小 [self.placeholderLabel sizeToFit]; self.placeholderLabel.center = self.textView.center; } /** textView文本内容改变时回调 */ - (void)textViewDidChange:(UITextView *)textView{ // 计算高度 CGSize size = CGSizeMake(self.textView.frame.size.width, CGFLOAT_MAX); NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:self.textView.font,NSFontAttributeName, nil]; CGFloat curheight = [textView.text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil].size.height; // 如果高度小于初始化时的高度,则不赋值(仍采用最初的高度) if (curheight < _initHeight) { self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, _initHeight); self.textView.frame = CGRectMake(self.textView.frame.origin.x, self.textView.frame.origin.y, self.textView.frame.size.width, _initHeight); }else{ // 重新给frame赋值(改变高度) self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, curheight+20); self.textView.frame = CGRectMake(self.textView.frame.origin.x, self.textView.frame.origin.y, self.textView.frame.size.width, curheight+20); } // 如果文本为空,显示placeholder if (textView.text.length == 0) { self.placeholderLabel.hidden = NO; self.placeholderLabel.center = self.textView.center; }else{ self.placeholderLabel.hidden = YES; } } @end
使用示例:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. CQTextView *textView = [[CQTextView alloc]initWithFrame:CGRectMake(90, 90, 100, 30)]; [self.view addSubview:textView]; textView.backgroundColor = [UIColor redColor]; textView.font = [UIFont systemFontOfSize:20]; textView.placeholder = @"ss"; }
注意:
光标的位置还需要调整一下,不然不居中,要回到原位。
[textView setContentOffset:CGPointZero animated:YES]; [textVeiw scrollRangeToVisible:textView.selectedRange];
相关文章推荐
- JAVA中int、String的类型相互转换
- dubbo+zookeeper构建高可用分布式集群
- 如果利用1台机器,启动多个hadoop节点
- 不明觉厉的16进制字符串转int
- gulp-uglify 与gulp.watch()配合使用时报错(重复压缩问题)
- C语言基础学习基本数据类型-int类型与int变量
- Maven学习
- 移植mavlink协议到STM32详细教程
- 模块加载(require)及定义(define)时的路径
- 内核定时器timer_list使用
- 【Java菜鸟 05】java中的 FileWriter类 和 FileReader类的一些基本用法
- 开始了第一次 就会有最后一次 也许你的第一次就是你的最后一次
- Android Studio 2.1.3 在gradle-experimental下编译使用OpenCV
- hdu 1232 畅通工程
- Web开发中一个完整的用户登陆(固化)
- B树、B-树、B+树、B*树
- 携程大数据实践:高并发应用架构及推荐系统案例
- css3浏览器前缀 -mos/-webkit/-o/-ms
- android 界面布局例子
- 166. Fraction to Recurring Decimal -- 将除法的商表示成字符串(循环节用括号表示)