UITextView更具内容来改变高度

2017-01-02  本文已影响0人  WeiSL

textView.delegate = self;

-(void)textViewDidChange:(UITextView *)textView{

static CGFloat maxHeight =60.0f;

CGRect frame = textView.frame;

CGSize constraintSize = CGSizeMake(frame.size.width, MAXFLOAT);

CGSize size = [textView sizeThatFits:constraintSize];

if (size.height<=frame.size.height) {

size.height=frame.size.height;

}else{

if (size.height >= maxHeight)

{

size.height = maxHeight;

textView.scrollEnabled = YES;  // 允许滚动

}

else

{

textView.scrollEnabled = NO;    // 不允许滚动

}

}

textView.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, size.height);

}

上一篇下一篇

猜你喜欢

热点阅读