高度根据输入内容变化输入框,我们在很多的应用上都可以见到,如微信、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. 关于PCB布线的顺序到底是怎样才合理?

    有人说先布好电源线和地线,让它们尽量靠近走,然后再考虑信号线:也有人说先布好关键的信号线,然后再走电源和地线:还有人说先布好电源线,再布信号线,地线最后布.到底怎么样才算好呢?或者说,一般应按照什么顺 ...

  2. nyoj 14 会场安排问题

    会场安排问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 学校的小礼堂每天都会有许多活动,有时间这些活动的计划时间会发生冲突,需要选择出一些活动进行举办.小刘的工 ...

  3. MapReduce的流程

    1. Inputformat会从job的INPUT_DIR目录下读入待处理的文件,检查输入的有效性并将文件切分成InputSplit列表.Job实例可以通过setInputFormatClass(Cl ...

  4. Spring ProxyFactory

    ProxyFactory 是 Spring AOP的实现方式之一.下面介绍下ProxyFactory的用法. 1.接口定义 public interface UserReadService { pub ...

  5. BZOJ [ZJOI2008]泡泡堂BNB 贪心

    [ZJOI2008]泡泡堂BNB Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/proble ...

  6. [React Native] Complete the Notes view

    In this final React Native lesson of the series we will finalize the Notes view component and squash ...

  7. 怎样加入� android private libraries 中的包的源码

    先上图: 这里以加入� afinal_0.5.1_bin.jar 为例. 第一步:加入�jar包到libs里面,系统自己主动把jar载入到android private libraries中: 第二步 ...

  8. Struts2 中的数据传输

    1.     如何将参数从界面传递到Action? 你可以把Struts2中的Action看做是Struts1的Action+ActionForm,即只需在Action中定义相关的属性(要有gette ...

  9. Hadoop可视化与交互式工具:Zeppelin和Hue

    https://yq.aliyun.com/articles/42282?spm=5176.team18.teamshow1.19.9TkKmZ#rd

  10. Android Studio无法启动 打开, Android Studio gradle下载不了

    Google在2013年I/O大会上发布了Android Studio,AndroidStudio是一个基于IntelliJ思想的新的Android开发工具.下面介绍一下Android Studio安 ...