如何調整UITextView的內容大小?有什么好方法來調整UITextView以符合它的內容?比如說我有一個UITextView其中包含一行文本:"Hello world"然后,我添加另一行文本:"Goodbye world"可可有什么好辦法rect它將容納文本視圖中的所有行,這樣我就可以相應地調整父視圖了嗎?作為另一個例子,查看Calendar應用程序中事件的Notes字段-注意單元格(以及UITextView它包含)展開,以保存注釋字符串中的所有文本行。
2 回答

犯罪嫌疑人X
TA貢獻2080條經驗 獲得超4個贊
- (void)textViewDidChange:(UITextView *)textView{ CGFloat fixedWidth = textView.frame.size.width; CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)]; CGRect newFrame = textView.frame; newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height); textView.frame = newFrame;}
let fixedWidth = textView.frame.size.width let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))textView.frame. size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)
textview.scrollEnabled = NO;

慕虎7371278
TA貢獻1802條經驗 獲得超4個贊
這不再適用于iOS 7或更高版本。
UITextView
UITextView
contentSize
.
CGRect frame = _textView.frame;frame.size.height = _textView.contentSize.height;_textView.frame = frame;
contentSize
UITextView
addSubview
frame.size
sizeThatFits
constant
CGSize sizeThatShouldFitTheContent = [_textView sizeThatFits:_textView.frame.size]; heightConstraint.constant = sizeThatShouldFitTheContent.height;
heightConstraint
[self.textView sizeToFit];
CGRect _f = self.mainPostText.frame;_f.size.height = self.mainPostText.contentSize.height;self.mainPostText.frame = _f;
- 2 回答
- 0 關注
- 855 瀏覽
添加回答
舉報
0/150
提交
取消