UITextView 高度自适应

2021-09-16  本文已影响0人  iOS乐乐
- (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);
    
}
上一篇下一篇

猜你喜欢

热点阅读