第十二篇、高度自适应的textView
高度根据输入内容变化输入框,我们在很多的应用上都可以见到,如微信、QQ聊天,QQ空间评论等等,该输入框可以用xib,纯代码编写,但是个人觉得纯代码编写用起来更加方便一些。
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(, , frame.size.width - , 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+);
self.textView.frame = CGRectMake(self.textView.frame.origin.x, self.textView.frame.origin.y, self.textView.frame.size.width, curheight+);
}
// 如果文本为空,显示placeholder
if (textView.text.length == ) {
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(, , , )];
[self.view addSubview:textView];
textView.backgroundColor = [UIColor redColor];
textView.font = [UIFont systemFontOfSize:];
textView.placeholder = @"ss";
}
注意:
光标的位置还需要调整一下,不然不居中,要回到原位。
[textView setContentOffset:CGPointZero animated:YES];
[textVeiw scrollRangeToVisible:textView.selectedRange];
第十二篇、高度自适应的textView的更多相关文章
- Python开发【第二十二篇】:Web框架之Django【进阶】
Python开发[第二十二篇]:Web框架之Django[进阶] 猛击这里:http://www.cnblogs.com/wupeiqi/articles/5246483.html 博客园 首页 ...
- 解剖SQLSERVER 第十二篇 OrcaMDF 行压缩支持(译)
解剖SQLSERVER 第十二篇 OrcaMDF 行压缩支持(译) http://improve.dk/orcamdf-row-compression-support/ 在这两个月的断断续续的开发 ...
- 第十二篇 SQL Server代理多服务器管理
本篇文章是SQL Server代理系列的第十二篇,详细内容请参考原文 在这一系列的上一篇,我们查看了维护计划,一个维护计划可能会创建多个作业,多个计划.你还简单地看了SSIS子系统,并查看了维护计划作 ...
- 第十二篇 Integration Services:高级日志记录
本篇文章是Integration Services系列的第十二篇,详细内容请参考原文. 简介在前一篇文章我们配置了SSIS内置日志记录,演示了简单和高级日志配置,保存并查看日志配置,生成自定义日志消息 ...
- Python之路【第十二篇】:JavaScrpt -暂无内容-待更新
Python之路[第十二篇]:JavaScrpt -暂无内容-待更新
- 【译】第十二篇 Integration Services:高级日志记录
本篇文章是Integration Services系列的第十二篇,详细内容请参考原文. 简介在前一篇文章我们配置了SSIS内置日志记录,演示了简单和高级日志配置,保存并查看日志配置,生成自定义日志消息 ...
- 【译】第十二篇 SQL Server代理多服务器管理
本篇文章是SQL Server代理系列的第十二篇,详细内容请参考原文 在这一系列的上一篇,我们查看了维护计划,一个维护计划可能会创建多个作业,多个计划.你还简单地看了SSIS子系统,并查看了维护计划作 ...
- 跟我学SpringCloud | 第十二篇:Spring Cloud Gateway初探
SpringCloud系列教程 | 第十二篇:Spring Cloud Gateway初探 Springboot: 2.1.6.RELEASE SpringCloud: Greenwich.SR1 如 ...
- Egret入门学习日记 --- 第十二篇(书中 5.1节 内容)
第十二篇(书中 5.1节 内容) 昨天把 第4章完成了. 今天来看第5章. 接下来是 5.1节 的内容. 总结一下 5.1节 的重点: 1.如何制作一个公用按钮皮肤. 跟着做: 重点1:如何制作一个公 ...
- Spring Cloud第十二篇 | 消息总线Bus
本文是Spring Cloud专栏的第十二篇文章,了解前十一篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring ...
随机推荐
- 修改cmd字体为Consolas
windows下的cmd窗口默认的字体有点难看,长时间使用操作nodejs有点小疲劳,可以修改注册表替换字体为Consolas,并且可以全屏cmd窗口,代码如下: Windows Registry E ...
- cocos2d-x 手势之简单实现
转自:http://blog.sina.com.cn/s/blog_61ece099010187tl.html 手势之前也发过一篇,但是我感觉那个还不够轻巧. 而且大多数游戏里面不会有那么复杂的手势, ...
- Oracle用户及角色的权限管理[Oracle基础]
1.查看全部用户: select * from dba_users; select * from all_users; select * from user_users; 2.查看用户或角 ...
- android常见错误-
将library中的报错项删除,然后点击[add]正确的appcompat
- 关于【cocos2dx-3.0beta-制作flappybird】教程在3.2project中出现找不到CCMenuItem.h的解决方法
文章原文:http://blog.csdn.net/kantian_/article/details/36187141 作者升级源码.能够在3.1平台下执行. 我的是vs2013+cocos2dx-3 ...
- JavaScript学习笔记之下拉选择框的操作
对于下拉框的操作十分繁多,这几天项目须要就总结一下 一.动态构建option 有时候我们须要动态构建下拉选择框里面的值,这里我们就要用到 var varItem = new Option(" ...
- PHP中获取中英文混合字符串长度[主要是指个数,而不是字符串长度](转)
今晚在写框架的表单验证类时,需要判断某个字符串长度是否在指定区间内,很自然地,想到了PHP中的strlen函数. $str = 'Hello world!'; echo strlen($str); ...
- Opencv cvCircle函数
cvCircle(CvArr* img, CvPoint center, int radius, CvScalar color, int thickness=1, int lineType=8, in ...
- VBScript
VBScript then PrintWMIErrorthenExit Err.Description, Err.Number else ifnot boolCathiMode then wscrip ...
- 使用RecyclerView实现瀑布流的效果
主函数: public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; privat ...