美文网首页
UITextView高度计算

UITextView高度计算

作者: 大浪捉鱼 | 来源:发表于2022-03-16 14:07 被阅读0次

    需求:用UITextView作为输入框,没有输入或输入一行以内的时候,textView的高度是一行的高度,随着输入内容的增加,textview的高度也跟着增加,但高度最大是能显示3行的高度,然后textview内部滚动显示。另外,textview可控制文字的行间距。

    1、textView的设置
    重点是textContainerInset和textContainer.lineFragmentPadding的设置,查看sdk说明文档,textContainerInset的默认值是(8, 0, 8, 0),textContainer.lineFragmentPadding的默认值是5,这里为了方便计算,统一设为0,去掉textView里文字区域四周的空隙

    - (UITextView *)textView {
        if (!_textView) {
            _textView = [[UITextView alloc] init];
            _textView.font = [UIFont systemFontOfSize:14];
            _textView.textColor = kAppearanceColor([UIColor colorWithHex:0x4D4D4D], [UIColor colorWithHex:0xE6E6E6]);
            _textView.tintColor = [UIColor colorWithHex:0x7147FF];
            _textView.returnKeyType = UIReturnKeySend;
            _textView.backgroundColor = [UIColor clearColor];
            _textView.delegate = self;
            _textView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);
            _textView.textContainer.lineFragmentPadding = 0;
            
            self.placeHolderLabel = [[UILabel alloc] init];
            self.placeHolderLabel.numberOfLines = 1;
            self.placeHolderLabel.font = [UIFont systemFontOfSize:14];
            self.placeHolderLabel.textColor= [UIColor colorWithHex:0x999999];
           [_textView addSubview:self.placeHolderLabel];
           [_textView setValue:self.placeHolderLabel forKey:@"_placeholderLabel"];
        }
        return _textView;
    }
    

    2、textView的布局

        [self.contentView addSubview:self.textView];
        [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(weakSelf.textBgView.mas_left).mas_offset(14);
            make.right.mas_equalTo(weakSelf.textBgView.mas_right).mas_offset(-14);
            make.top.mas_equalTo(weakSelf.textBgView.mas_top).mas_offset(9);
            make.bottom.mas_equalTo(weakSelf.textBgView.mas_bottom).mas_offset(-9);
        }];
    

    3、输入时动态计算和调整高度

    - (void)textViewDidChange:(UITextView *)textView {
        textView.attributedText = [[NSAttributedString alloc] initWithString:textView.text attributes:[self attributes]];
        
        CGFloat oldHeight = CGRectGetHeight(self.textBgView.frame);
        CGFloat height = [self calculateTextBgHeight:textView.text];
        if (fabs(height - oldHeight)>0.01) {
            [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(height + 10*2);
            }];
        } 
    }
    
    - (CGFloat)calculateTextBgHeight:(NSString *)content {
        NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:content attributes:[self attributes]];
        CGFloat maxWidth = kDeviceWidth - 87 - 76 - self.textView.textContainer.lineFragmentPadding *2;
        CGRect rect = [attributedText boundingRectWithSize:CGSizeMake(maxWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil];
        CGFloat textHeight = ceilf(rect.size.height);
        CGFloat height = textHeight + 9*2;
        return MIN(MAX(height, 38), 78);
    }
    
    - (NSDictionary *)attributes {
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineHeightMultiple = 1.14;
        return @{NSFontAttributeName:[UIFont systemFontOfSize:14],
                 NSParagraphStyleAttributeName: paragraphStyle
               };
    }
    
    

    参考资料

    https://blog.csdn.net/weixin_33946605/article/details/91941424
    https://www.jianshu.com/p/1d7d8cf9b1fe
    https://www.jianshu.com/p/2d9fde661004
    https://www.cnblogs.com/wgb1234/p/12449905.html

    相关文章

      网友评论

          本文标题:UITextView高度计算

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