动态改变UITextView高度

mark:

iOS:如何优雅的让UITextView根据输入文字实时改变高度

摘要分析:

1.这个是一边输入,一边改的

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    CGRect frame = textView.frame;

    
    float height;
    if ([text isEqual:@""]) {
        
        if (![textView.text isEqualToString:@""]) {
            
            //    /*
            //     按删除键的时候,text的文字为@"",导致删到换行时,并没有立马删,高度不同步,故强行删,但是容易越界,故要做判断
            //     */
            height = [ self heightForTextView:textView WithText:[textView.text substringToIndex:[textView.text length] - 1]];
            
        }else{
            
            height = [ self heightForTextView:textView WithText:textView.text];
        }
    }else{
        
        //    /*
        //     textView.text和text,为了一输入就立马同步到textView里面,不等结束,从而解决输入和变高度不能立马同步
        //     */
        
        height = [self heightForTextView:textView WithText:[NSString stringWithFormat:@"%@%@",textView.text,text]];
    }
    
    frame.size.height = height;
    __weak typeof(self)weakSelf = self;
    [UIView animateWithDuration:0.5 animations:^{
        
        textView.frame = frame;
  //这方法是要最后变一下父view的frame的
[weakSelf footerViewContent];
        
    } completion:nil];
    
    return YES;
}

- (float) heightForTextView: (UITextView *)textView WithText: (NSString *) strText{
    CGSize constraint = CGSizeMake(textView.contentSize.width , CGFLOAT_MAX);
    CGRect size = [strText boundingRectWithSize:constraint
                                        options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                     attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14]}
                                        context:nil];
    float textHeight = size.size.height + 10.0;
    return textHeight;
}

2.调用

调动这个方法是针对,一开始适配默认值尺寸的

- (void)updateAddressTextViewAndFooterViewHeight{
    
    float height = [self heightForTextView:self.addressTextView WithText:self.addressTextView.text];
    [self.addressTextView setYh_height:height];
    [self footerViewContent];
}

3.无论哪种情况,最终都是要更新一下父view的frame的


封装版

h

#import <UIKit/UIKit.h>

@class XYCustomTextView;

@protocol XYCustomTextViewDelegate <NSObject>

- (void)customTextViewNewHeight:(CGFloat)height textView:(XYCustomTextView *)textView;

@end

@interface XYCustomTextView : UITextView

@property (assign, nonatomic) id<XYCustomTextViewDelegate> customTextViewDelegate;
//初始设置
- (instancetype)initWithFrame:(CGRect)frame
              placeHolderText:(NSString *)placeHolderText
                textAlignment:(NSTextAlignment)textAlignment
       customTextViewDelegate:(id<XYCustomTextViewDelegate>)customTextViewDelegate;
//一开始要给这个值又要适配高度的情况
- (void)updateDescribeHeightWithDescribeForBegin:(NSString *)beginDescribe;


@end

m

#import "XYCustomTextView.h"

@interface XYCustomTextView()<UITextViewDelegate>

@property(strong, nonatomic) UILabel *textViewPlaceholderLabel;

@end

@implementation XYCustomTextView

//最小高度
#define minLineHeight 40

- (instancetype)initWithFrame:(CGRect)frame
              placeHolderText:(NSString *)placeHolderText
                textAlignment:(NSTextAlignment)textAlignment
       customTextViewDelegate:(id<XYCustomTextViewDelegate>)customTextViewDelegate

{
    
    if (self = [super initWithFrame:frame]) {

        self.font = [UIFont systemFontOfSize:14];
        self.textAlignment = textAlignment;
        self.backgroundColor = [UIColor yellowColor];
        //placeHolderLabel
        _textViewPlaceholderLabel = [UILabel new];
        _textViewPlaceholderLabel.font = [UIFont systemFontOfSize:14];
        _textViewPlaceholderLabel.textAlignment = textAlignment;
        _textViewPlaceholderLabel.textColor = [UIColor lightGrayColor];
        _textViewPlaceholderLabel.text = placeHolderText;
        self.delegate = self;
        [self addSubview:_textViewPlaceholderLabel];
        
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    _textViewPlaceholderLabel.frame = CGRectMake(0, 0, self.frame.size.width - 5 , self.frame.size.height);
    
}

- (void)updateDescribeHeightWithDescribeForBegin:(NSString *)beginDescribe{
    
    _textViewPlaceholderLabel.hidden = YES;

    self.text = beginDescribe;
    
    float height = [self heightForTextView:self WithText:beginDescribe];
    //更新高度
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, height);

 //更新外部的高度
    [self updateOutsideHeight:height];
}

#pragma mark ------------------delegate------------


- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    
    NSLog(@"subView__text___%@",text);
    CGRect frame = textView.frame;
    
    _textViewPlaceholderLabel.hidden = YES;
    
    if (range.length == 0 &&
        range.location == 0 &&
        [text isEqual:@""]){
        
        _textViewPlaceholderLabel.hidden = NO;

    }
    
    float height;
    if ([text isEqual:@""]) {
        

        
        if (![textView.text isEqualToString:@""]) {
            
            height = [ self heightForTextView:textView WithText:[textView.text substringToIndex:[textView.text length] - 1]];
            
        }else{
            
            height = [ self heightForTextView:textView WithText:textView.text];
        }
    }else{
        
        
        height = [self heightForTextView:textView WithText:[NSString stringWithFormat:@"%@%@",textView.text,text]];
    }
    
    frame.size.height = height;
    [UIView animateWithDuration:0.5 animations:^{
        
        textView.frame = frame;
        
    } completion:nil];
    
    //更新外部的高度
    [self updateOutsideHeight:height];
    
    return YES;
}

- (void)updateOutsideHeight:(CGFloat)height{
    if ([_customTextViewDelegate respondsToSelector:@selector(customTextViewNewHeight:textView:)]){
        
        [_customTextViewDelegate customTextViewNewHeight:height textView:self];
    }

}
- (float) heightForTextView: (UITextView *)textView WithText: (NSString *) strText{
    CGSize constraint = CGSizeMake(textView.contentSize.width , CGFLOAT_MAX);
    CGRect size = [strText boundingRectWithSize:constraint
                                        options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                     attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14]}
                                        context:nil];
    float textHeight = size.size.height + 10.0;
    //最小值
    if (textHeight < minLineHeight){
        textHeight = minLineHeight;
    }
    return textHeight;
}

@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,073评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,592评论 4 61
  • 今天学习 1.【DOM转CANVAS】 大致了解了canvas可以将dom转成图片,还有二维码生成 今日看法 感悟...
    陈式坚阅读 1,171评论 0 0
  • 来了大学大半年了,记得曾经埋头苦干,记得曾经在题海中攀爬,记得曾经和拥有相同梦想的人一起前进,三年高中使大多数的...
    手边眼边阅读 1,490评论 1 1
  • 在一恍惚间, 迎春花谢了, 杏花谢了, 桃花也谢了, 还有……还有……都谢了。 在那个细雨朦胧的春曰, 还没来及观...
    菩提果zk张珂阅读 1,508评论 0 0

友情链接更多精彩内容