IOS7以后ContentSize的直接计算会出现误差,需要通过layoutManager来获得:
具体代码如下:
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
CGRect textFrame=[[self.textView layoutManager]usedRectForTextContainer:[self.textView textContainer]];
height = textFrame.size.height;
}else {
height = self.textView.contentSize.height;
}
</pre><pre name="code" class="objc">计算出contentSize的话,可以借助contentSize动态计算IOS中UITextView的文本内容行数,如下:
</pre><pre name="code" class="objc">
numLines = textView.contentSize.height/textView.font.lineHeight;</span>
最后再结合UITextViewDelgate可以实现QQ消息输入框随输入内容变化调整输入框的高度。