第十二篇、高度自适应的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 ...
随机推荐
- HDU 2199 Can you solve this equation? (二分 水题)
Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- JavaScript 的原型对象 Prototype
在 JavaScript 中,每当定义一个对象(或函数)时候,对象中都会包含一些预定义的属性,其中一个属性就是原型对象 prototype. var myObject = function( name ...
- 关于iOS中SQLITE句柄的使用的细节
1.设计思想:给SQLITE进行封装,利用定义的类别实现方法的简洁,以便达到低耦合效果 控制器代码: #import "ViewController.h" #import &quo ...
- LocalDB 的创建与迁移
首先创建对应的对象 public class Movie { public int ID { get; set; } public string Title { get; set; } public ...
- 本地或者是koala软件编译less文件为css
背景: 事情的起因是这般的,平时工作是在线上办公,样式是使用less来写,于是乎,这样我从线上download下来的less文件无法直接在自己的本地环境运行.有一个问题就是我要把less文件先编译成c ...
- 利用存储过程将表中的数据生成Insert语句
1.创建存储过程 CREATE PROC [dbo].[sp_get_InsertSql] @dbName VARCHAR ( )= '' , -- 数据库名称 @tabList VARCHAR ( ...
- Ubuntu安装和配置redis
1.用root用户登录 2.执行 sudo apt-get install redis-server 部分截图
- Codeforces Round #321 (Div. 2) B. Kefa and Company 二分
B. Kefa and Company Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/pr ...
- 基于Android 平台简易即时通讯的研究与设计[转]
摘要:论文简单介绍Android 平台的特性,主要阐述了基于Android 平台简易即时通讯(IM)的作用和功能以及实现方法.(复杂的通讯如引入视频音频等可以考虑AnyChat SDK~)关键词:An ...
- android学习日记03--常用控件Dialog
常用控件 9.Dialog 我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框 对话框,要创建对话框之前首先要创建Bui ...