原文地址:http://www.reibang.com/p/55d98e8f3e61
原文底下評(píng)論有提到iOS8.0的不能滑動(dòng)的bug.廢話不多說,直接上代碼
修改一個(gè)方法就可以搞掂.
@property (nonatomic,assign) BOOL flag;
- (void)textDidChange
{
// 占位文字是否顯示
self.placeholderView.hidden = self.text.length > 0;
NSInteger height = ceilf([self sizeThatFits:CGSizeMake(self.bounds.size.width, MAXFLOAT)].height);
if (_textH != height) { // 高度不一樣念秧,就改變了高度
// 最大高度吕嘀,可以滾動(dòng)
self.scrollEnabled = height > _maxTextH && _maxTextH > 0;
_textH = height;
if (height > _maxTextH) {
_textH = _maxTextH;
}
if (_yz_textHeightChangeBlock && self.scrollEnabled == NO) {
_yz_textHeightChangeBlock(self.text,height);
[self scrollRangeToVisible:NSMakeRange(0,0)];
self.placeholderView.frame = self.bounds;
_flag = NO;
}
if (_yz_textHeightChangeBlock && self.scrollEnabled == YES){
self.nim_height = _maxTextH;
if (!_flag) {
[self scrollRangeToVisible:NSMakeRange(0, self.text.length)];
_flag = YES;
}
}
[self.superview layoutIfNeeded];
}
}