UITextView输入时高度自适应(优化增强版)

另外一个三方库 《一分钟集成类似抖音、头条、腾讯视频、网易新闻等常用标题栏》

最新更新:💪之前代码是基于UITextView进行的封装,侵入性较强,不方便使用,现使用Category重构代码,支持Cocoapods
项目地址

DECLARATION:

Because the original code use inheritance which is too intrusive, now I use category to reconstruct the code, and released new version 。

Table of contents

CocoaPods:

  • For iOS8+:
use_frameworks!
target '<Your Target Name>' do
    pod 'UITextView+CMInputView'
end

Manually:

  • Drag all files under CMInputView/CMInputView/Class folder into your project.

Pre-knowledge

Example

Firstly, you need import the .h file.

if cocoaposd:

#import <UITextView+CMInputView.h>

if manually:

#import "UITextView+CMInputView.h"

Then create UITextView and use this category to configure .

    UITextView * inputView = [[UITextView alloc]initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 55)];
    inputView.font = [UIFont systemFontOfSize:18];
    
    inputView.cm_placeholder =  @"UITextView+CMInputView";
    inputView.cm_placeholderColor = [UIColor redColor];
    inputView.cm_maxNumberOfLines = 3;
    
    [self.view addSubview:inputView];

If you want to make the UITextView break line automatically ,you can write like this:

    UITextView * inputView = [[UITextView alloc]initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 55)];
    inputView.font = [UIFont systemFontOfSize:18];
   
    inputView.cm_placeholder =  @"UITextView+CMInputView";
    inputView.cm_placeholderColor = [UIColor redColor];
    inputView.cm_autoLineBreak = YES;
    
    [self.view addSubview:inputView];

Congratulations! You're done. 🎉

Notes

  • The category do not support constrained layout,please use carefully !
  • Once you set cm_maxNumberOfLines ,the effect always works,even if you set YES to cm_autoLineBreak

Support this repo

  • ★Star this repo
  • If you find somes bugs or need some new functions,please Issue me

License

"UITextView+CMInputView" is available under the MIT license. See the LICENSE file for more info.

接上篇这段时间比较忙,说好的给写一个UITextView的demo,但是无奈一直没有时间,今天终于抽时间完成了,基本要求的功能都具备。

1.png

首先回答一下@RicoCL的问题,一眨眼已经将近40天了,实在是不好意思,其实原理很简单,只要在当前文字高度 >= textView最大高度时,将textView的scrollEnable设置为YES即可,删除时,在当前文字高度 < textView最大高度时,设置为NO即可。

效果图镇楼

输入框效果图.gif

1 需求分析

平时经常用到UITextView时,经常会有这么几个问题:

  • placeholder的设置
  • placeholder字体颜色的设置
  • placeholder字体大小的设置
  • 圆角以及边框的设置
  • UITextView行数限制,达到最大高度时,可以滚动查看之前内容
  • UITextView输入时高度自适应,包括输入以及删除时
  • UITextView常常不是单独出现,例如微信输入框右侧的声音按钮,+号按钮等等。
针对上面的几个需求,逐一分析:
  • 其中placeholder的相关属性(placeholder,字体颜色,字体大小)是通过在另外一个大小相等的UITextView *placeholderView的相关属性实现的,同等大小的_placeholderView可以完美的与之重合,是设置作为设置placeholderView最好选择。重写公有属性的setter方法,设置_placeholderView的相关属性
  • 圆角以及边框设置通过layer属性实现;
  • UItextView文本最大行数公开属性,供外界调用。高度自适应,给UITextView设置监听,监听方法根据文本内容动态决定UITextView是否可以滚动。需求中因为UITextView常常不是单独出现,父视图有可能是一个View,并在父视图之间设置间距以及约束,因此设置一个block动态的根据需求改变父视图的高度。

2 代码展示

demo中的注释比较项目,有需要的可以直接调至文末直接下载。
CMInputView.h文件

#import <UIKit/UIKit.h>

typedef void(^CM_textHeightChangedBlock)(NSString *text,CGFloat textHeight);

@interface CMInputView : UITextView

/**
 *  占位文字
 */
@property (nonatomic, strong) NSString *placeholder;

/**
 *  占位文字颜色
 */
@property (nonatomic, strong) UIColor *placeholderColor;

/**
 *  占位符字体大小
 */
@property (nonatomic,strong) UIFont *placeholderFont;

/**
 *  textView最大行数
 */
@property (nonatomic, assign) NSUInteger maxNumberOfLines;

/**
 *  文字高度改变block → 文字高度改变会自动调用
 *  block参数(text) → 文字内容
 *  block参数(textHeight) → 文字高度
 */
@property (nonatomic, strong) CM_textHeightChangedBlock textChangedBlock;
/**
 *  设置圆角
 */
@property (nonatomic, assign) NSUInteger cornerRadius;

- (void)textValueDidChanged:(CM_textHeightChangedBlock)block;

@end

CMTextView.m文件中核心代码的实现

- (void)textDidChange
{
    // 根据文字内容决定placeholderView是否隐藏
    self.placeholderView.hidden = self.text.length > 0;
    
    NSInteger height = ceilf([self sizeThatFits:CGSizeMake(self.bounds.size.width, MAXFLOAT)].height);
    
    if (_textH != height) { // 高度不一样,就改变了高度
        
        // 当高度大于最大高度时,需要滚动
        self.scrollEnabled = height > _maxTextH && _maxTextH > 0;
        
        _textH = height;
        
        //当不可以滚动(即 <= 最大高度)时,传值改变textView高度
        if (_textChangedBlock && self.scrollEnabled == NO) {
            _textChangedBlock(self.text,height);
            
            [self.superview layoutIfNeeded];
            self.placeholderView.frame = self.bounds;

        }
    }
}

3 使用注意

_placeholderView的初始化是在_placeholder的setter方法中实现的,其中font的设置是与UITextViewfont是同等大小的,因此placeholder属性的设置需要在设置UITextViewfont之后才会生效,否则为系统默认字体大小;当然,不需要先后顺序,通过placeholderFont属性的设置也可以实现。

Demo下载 请戳这里 欢迎star

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容