高度根据输入内容变化输入框,我们在很多的应用上都可以见到,如微信、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的更多相关文章

  1. Python开发【第二十二篇】:Web框架之Django【进阶】

    Python开发[第二十二篇]:Web框架之Django[进阶]   猛击这里:http://www.cnblogs.com/wupeiqi/articles/5246483.html 博客园 首页 ...

  2. 解剖SQLSERVER 第十二篇 OrcaMDF 行压缩支持(译)

    解剖SQLSERVER 第十二篇   OrcaMDF 行压缩支持(译) http://improve.dk/orcamdf-row-compression-support/ 在这两个月的断断续续的开发 ...

  3. 第十二篇 SQL Server代理多服务器管理

    本篇文章是SQL Server代理系列的第十二篇,详细内容请参考原文 在这一系列的上一篇,我们查看了维护计划,一个维护计划可能会创建多个作业,多个计划.你还简单地看了SSIS子系统,并查看了维护计划作 ...

  4. 第十二篇 Integration Services:高级日志记录

    本篇文章是Integration Services系列的第十二篇,详细内容请参考原文. 简介在前一篇文章我们配置了SSIS内置日志记录,演示了简单和高级日志配置,保存并查看日志配置,生成自定义日志消息 ...

  5. Python之路【第十二篇】:JavaScrpt -暂无内容-待更新

    Python之路[第十二篇]:JavaScrpt -暂无内容-待更新

  6. 【译】第十二篇 Integration Services:高级日志记录

    本篇文章是Integration Services系列的第十二篇,详细内容请参考原文. 简介在前一篇文章我们配置了SSIS内置日志记录,演示了简单和高级日志配置,保存并查看日志配置,生成自定义日志消息 ...

  7. 【译】第十二篇 SQL Server代理多服务器管理

    本篇文章是SQL Server代理系列的第十二篇,详细内容请参考原文 在这一系列的上一篇,我们查看了维护计划,一个维护计划可能会创建多个作业,多个计划.你还简单地看了SSIS子系统,并查看了维护计划作 ...

  8. 跟我学SpringCloud | 第十二篇:Spring Cloud Gateway初探

    SpringCloud系列教程 | 第十二篇:Spring Cloud Gateway初探 Springboot: 2.1.6.RELEASE SpringCloud: Greenwich.SR1 如 ...

  9. Egret入门学习日记 --- 第十二篇(书中 5.1节 内容)

    第十二篇(书中 5.1节 内容) 昨天把 第4章完成了. 今天来看第5章. 接下来是 5.1节 的内容. 总结一下 5.1节 的重点: 1.如何制作一个公用按钮皮肤. 跟着做: 重点1:如何制作一个公 ...

  10. Spring Cloud第十二篇 | 消息总线Bus

    ​ ​本文是Spring Cloud专栏的第十二篇文章,了解前十一篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring ...

随机推荐

  1. Razor模板引擎(C#版)语法

    1.简介: Razor 是一种标记语法,可以让您将基于服务器的代码(Visual Basic 和 C#)嵌入到网页中. 基于服务器的代码可以在网页传送给浏览器时,创建动态 Web 内容.当一个网页被请 ...

  2. Quartus II调用modelsim无缝仿真

    本篇文章为转载,写的不错,最近在学modelsim仿真,网上的教程很乱,把自己认为不错的整理贴出来,后面有机会会写个详细点的. Quartus 中调用modelsim的流程 1. 设定仿真工具 ass ...

  3. 【WIN32进阶之路】:内存映射文件

    第一章:源起  遇到一个问题,如果一个客户数据文件有2g大,客户要通过界面查询文件中的数据并用列表控件显示数据,要怎么处理这个文件才能让应用程序不会长时间无响应,客户感觉不到程序的卡顿? 第二章:解决 ...

  4. 查看linux中的TCP连接数【转】

     转自:http://blog.csdn.net/he_jian1/article/details/40787269 查看linux中的TCP连接数 本文章已收录于:   计算机网络知识库  分类: ...

  5. zigbee 学习记录之一:资料搜索

    先从网络来一段资料吧,在学习过程中慢慢整理资料. <由于zigbee 以cc2530 51单片机为基础,Zstack为源头,比较成熟了,学习和摸索就从Zstack开始吧.没有师傅,就有从网络上大 ...

  6. svn项目导入

    svn项目导入 在已建立好svnserverserver的情况下.且所用电脑上已装好svn,在电脑上导入svn项目 首先,新建目录.点击鼠标右键,选择TortoiseSVN–>export 然后 ...

  7. Aizu 2306 Rabbit Party DFS

    Rabbit Party Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view. ...

  8. 解决content is not allowed in prolog问题

    将xml文档用notepad++以UTF-8无BOM格式编码保存便可以了

  9. PAT 1016

    1016. Phone Bills (25) A long-distance telephone company charges its customers by the following rule ...

  10. C#_MVC3之使用Authorize简单的验证登录(一)

    第一步,自定义 AuthorizeAttribute. public class CustomAuthorizeAttribute : AuthorizeAttribute { public over ...