美文网首页Swift&Objective-C实用工具iOS学习笔记
UItextView自适应高度,一键搞定

UItextView自适应高度,一键搞定

作者: 我是七月 | 来源:发表于2018-06-11 22:44 被阅读12次
    奋斗的七月

    *******夜深了,直接上代码*******

    用法如下

        _inputView = [[CMInputView alloc]initWithFrame:CGRectMake(50, 100, 300, 40)];
    
        _inputView.font = [UIFont systemFontOfSize:18];
        _inputView.placeholder = @"测试文字";
        _inputView.cornerRadius = 4;
        _inputView.placeholderColor = [UIColor redColor];
        //_inputView.placeholderFont = [UIFont systemFontOfSize:22];
        _inputView.backgroundColor =[UIColor greenColor];
        [_inputView textValueDidChanged:^(NSString *text, CGFloat textHeight) {
            CGRect frame = _inputView.frame;
            frame.size.height = textHeight;
            _inputView.frame = frame;
        }];
        // 设置文本框最大行数
        _inputView.maxNumberOfLines = 4;
        [self.view addSubview:_inputView];
    

    CMInputView.h

    //
    //  CMInputView
    //
    
    #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
    
    

    CMInputView.m

    //
    //  CMInputView
    //
    
    #import "CMInputView.h"
    
    @interface CMInputView ()
    /**
     *  UITextView作为placeholderView,使placeholderView等于UITextView的大小,字体重叠显示,方便快捷,解决占位符问题.
     */
    @property (nonatomic, weak) UITextView *placeholderView;
    
    /**
     *  文字高度
     */
    @property (nonatomic, assign) NSInteger textH;
    
    /**
     *  文字最大高度
     */
    @property (nonatomic, assign) NSInteger maxTextH;
    
    
    @end
    
    @implementation CMInputView
    
    - (void)textValueDidChanged:(CM_textHeightChangedBlock)block{
        
        _textChangedBlock = block;
    
    }
    - (UITextView *)placeholderView
    {
        if (!_placeholderView ) {
            UITextView *placeholderView = [[UITextView alloc] initWithFrame:self.bounds];
            _placeholderView = placeholderView;
            //防止textView输入时跳动问题
            _placeholderView.scrollEnabled = NO;
            _placeholderView.showsHorizontalScrollIndicator = NO;
            _placeholderView.showsVerticalScrollIndicator = NO;
            _placeholderView.userInteractionEnabled = NO;
            _placeholderView.font =  self.font;
            _placeholderView.textColor = [UIColor lightGrayColor];
            _placeholderView.backgroundColor = [UIColor clearColor];
            [self addSubview:placeholderView];
        }
        return _placeholderView;
    }
    
    - (void)setMaxNumberOfLines:(NSUInteger)maxNumberOfLines
    {
        _maxNumberOfLines = maxNumberOfLines;
        
        /**
         *  根据最大的行数计算textView的最大高度
         *  计算最大高度 = (每行高度 * 总行数 + 文字上下间距)
         */
        _maxTextH = ceil(self.font.lineHeight * maxNumberOfLines + self.textContainerInset.top + self.textContainerInset.bottom);
    }
    
    - (void)setCornerRadius:(NSUInteger)cornerRadius
    {
        _cornerRadius = cornerRadius;
        self.layer.cornerRadius = cornerRadius;
    }
    
    /**
     *  通过设置placeholder设置私有属性placeholderView中的textColor
     */
    - (void)setPlaceholderColor:(UIColor *)placeholderColor
    {
        _placeholderColor = placeholderColor;
        
        self.placeholderView.textColor = placeholderColor;
    }
    /**
     *  通过设置placeholder设置私有属性placeholderView中的textColor
     */
    - (void)setPlaceholder:(NSString *)placeholder
    {
        _placeholder = placeholder;
        
        self.placeholderView.text = placeholder;
    }
    /**
     *  通过设置_placeholderFont设置私有属性placeholderView中的Font
    */
    - (void)setPlaceholderFont:(UIFont *)placeholderFont {
    
        _placeholderFont = placeholderFont;
        
        self.placeholderView.font = placeholderFont;
    }
    
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
        if (self = [super initWithFrame:frame]) {
            
            [self setup];
        }
        return self;
    }
    
    - (void)setup
    {
        
        self.scrollEnabled = NO;
        self.scrollsToTop = NO;
        self.showsHorizontalScrollIndicator = NO;
        self.enablesReturnKeyAutomatically = YES;
        self.layer.borderWidth = 1;
        self.layer.borderColor = [UIColor lightGrayColor].CGColor;
        //实时监听textView值得改变
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self];
    }
    
    - (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;
    
            }
        }
    }
    
    
    - (void)dealloc
    {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
    
    @end
    
    

    相关文章

      网友评论

      本文标题:UItextView自适应高度,一键搞定

      本文链接:https://www.haomeiwen.com/subject/bzskeftx.html