美文网首页
UITextView 高度自适应

UITextView 高度自适应

作者: iOS乐乐 | 来源:发表于2021-09-16 16:48 被阅读0次
    - (void)awakeFromNib {
        [super awakeFromNib];
        // Initialization code
        self.selectionStyle=UITableViewCellSelectionStyleNone;
        self.lineView.backgroundColor=LineColor;
        self.TV.delegate=self;
        self.TV.layer.borderWidth=2*PIX;
        self.TV.layer.borderColor=[UIColor lightGrayColor].CGColor;
        self.TV.clipsToBounds=YES;
        self.TV.layer.cornerRadius=5;
        self.TV.font=[UIFont systemFontOfSize:14];
        //加下面一句话的目的是,是为了调整光标的位置,让光标出现在UITextView的正中间
        self.TV.textContainerInset = UIEdgeInsetsMake(10, 5, 10, 5);
        DLog(@"1---->%f",self.TV.width);
    }
    
    
    //实现textViewDidChange方法:
    -(void)textViewDidChange:(UITextView *)textView{
        if (textView.text.length>0) {
            self.placeLabel.hidden=YES;
        }else{
            self.placeLabel.hidden=NO;
        }
        static CGFloat maxHeight =100.0f;
        static CGFloat minHeight =38.0f;
    
        CGRect frame = textView.frame;
        CGSize constraintSize = CGSizeMake(frame.size.width, MAXFLOAT);
        CGSize size = [textView sizeThatFits:constraintSize];
        //
        if (size.height<=frame.size.height) {
            if (size.height<minHeight) {
                size.height=minHeight;
            }
        }else{
            if (size.height >= maxHeight){
                size.height = maxHeight;
                textView.scrollEnabled = YES;  // 允许滚动
            }else{
                textView.scrollEnabled = NO;    // 不允许滚动
            }
        }
    
        self.TVHeight.constant = size.height;
        [self.tableView beginUpdates];
        [self.tableView endUpdates];
    
        self.TVBlock(textView);
        
    }
    

    相关文章

      网友评论

          本文标题:UITextView 高度自适应

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