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

    -255之间 14.2 CSS中的文字属性 属性名称                    属性值                       说明 font-style          norma ...

  2. Stage3D学习笔记(二):使用GPU绘制一个三角形

    我们需要使用到Adobe自家提供的AGALMiniAssembler代码类,可以在网下进行下载: 关于AGAL的入门知识可以参考下面的文章: AGAL介绍系列文章(第一部分)AGAL介绍系列文章(第二 ...

  3. MySQL 索引、视图、DML

    1.索引 索引是存放在模式(schema)中的一个数据库对象,索引的作用就是提高对表的检索查询速度, 索引是通过快速访问的方法来进行快速定位数据,从而减少了对磁盘的读写操作. 索引是数据库的一个对象, ...

  4. 使用ApplicationLoader中出现报错:The IPA is invalid. It does not inlude a Payload directory

    问题处理方法: 1.将achieve的.app后缀的软件包放在一个payload的文件夹中 2.压缩该文件夹,改变.zip后缀为.ipa 3.使用applicationLoader上传该文件  

  5. Java DESede用C++ Openssl实现

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  6. js url传值中文乱码之解决之道

    在websphere 中使用的是url=encodeURI(encodeURI(url)); //用了2次encodeURI 测试成功,第一次转换没有尝试, 处理方法一. js 程序代码:url=en ...

  7. 远程重启IIS服务

    方法一: $UserName = "administrator" $serverpass = "pass" $server = "10.4.19.60 ...

  8. HDU 5289 Assignment(2015 多校第一场二分 + RMQ)

    Assignment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

  9. android学习日记05--Activity间的跳转Intent实现

    Activity间的跳转 Android中的Activity就是Android应用与用户的接口,所以了解Activity间的跳转还是必要的.在 Android 中,不同的 Activity 实例可能运 ...

  10. .Net 与 Java 的服务接口相互调用

    本文介绍.Net 与 Java 相互调用的例子.下面的介绍主要包括三方面:一是通过常用Web服务进行相互调用,二是使用TCP/IP套接字进行相互调用,三是使用Remote实现远程对象相互调用. 首先说 ...