是否有一个好的方法来调整UITextView的大小以符合其内容?例如,我有一个包含一行文本的UITextView:
"Hello world"
然后我添加了另一行文本:
"Goodbye world"
在Cocoa Touch中是否有一个好的方法来获取矩形来保存文本视图中的所有行,这样我就可以相应地调整父视图?
另一个例子是,查看Calendar应用程序中用于事件的notes字段——注意单元格(以及它包含的UITextView)如何展开以保存notes字符串中的所有文本行。
是否有一个好的方法来调整UITextView的大小以符合其内容?例如,我有一个包含一行文本的UITextView:
"Hello world"
然后我添加了另一行文本:
"Goodbye world"
在Cocoa Touch中是否有一个好的方法来获取矩形来保存文本视图中的所有行,这样我就可以相应地调整父视图?
另一个例子是,查看Calendar应用程序中用于事件的notes字段——注意单元格(以及它包含的UITextView)如何展开以保存notes字符串中的所有文本行。
当前回答
更新
你需要做的关键事情是关闭UITextView中的滚动。
myTextView.scrollEnabled = @NO
原来的答案
要在UITableViewCell中创建一个动态大小的UITextView,我发现以下组合在Xcode 6和iOS 8 SDK中有效:
Add a UITextView to a UITableViewCell and constrain it to the sides Set the UITextView's scrollEnabled property to NO. With scrolling enabled, the frame of the UITextView is independent of its content size, but with scrolling disabled, there is a relationship between the two. If your table is using the original default row height of 44 then it will automatically calculate row heights, but if you changed the default row height to something else, you may need to manually switch on auto-calculation of row heights in viewDidLoad: tableView.estimatedRowHeight = 150; tableView.rowHeight = UITableViewAutomaticDimension;
对于只读动态大小的uitextview,就是这样。如果你允许用户在你的UITextView中编辑文本,你还需要:
实现UITextViewDelegate协议的textViewDidChange:方法,并告诉tableView在每次文本被编辑时重新绘制自己: - (void)textViewDidChange:(UITextView *)textView; { [tableView beginUpdates]; [tableView endUpdates]; } 别忘了在某个地方设置UITextView委托,在Storyboard或者tableView:cellForRowAtIndexPath:
其他回答
另一个方法是使用NSString方法查找特定字符串将占用的大小:
-(CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)大小
它返回符合给定字符串和给定字体的矩形的大小。传入一个具有所需宽度和最大高度的大小,然后您可以查看返回的适合文本的高度。还有一个版本允许您指定换行模式。
然后,您可以使用返回的大小来更改视图的大小以适应。
根据我有限的经验,
- (CGSize)sizeWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)lineBreakMode
不尊重换行符,所以你最终可以得到比实际需要的更短的CGSize。
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size
看起来很尊重换行符。
同样,文本也不是在UITextView顶部渲染的。在我的代码中,我将UITextView的新高度设置为比sizeOfFont方法返回的高度大24个像素。
我找到了一种方法,根据里面的文本调整文本字段的高度,并根据文本字段的高度在下面安排一个标签!这是代码。
UITextView *_textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, 300, 10)];
NSString *str = @"This is a test text view to check the auto increment of height of a text view. This is only a test. The real data is something different.";
_textView.text = str;
[self.view addSubview:_textView];
CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
_textView.frame = frame;
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(10, 5 + frame.origin.y + frame.size.height, 300, 20)];
lbl.text = @"Hello!";
[self.view addSubview:lbl];
这在ios7或更高版本上不再适用
实际上有一个非常简单的方法来调整UITextView的大小,使其达到内容的正确高度。它可以使用uitextviewcontentsize来完成。
CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
_textView.frame = frame;
需要注意的一件事是,只有在使用addSubview将UITextView添加到视图之后,正确的contentSize才可用。在此之前,它等于frame。size
如果开启自动布局,这将不起作用。对于自动布局,一般的方法是使用sizeThatFits方法并在高度约束上更新常量值。
CGSize sizeThatShouldFitTheContent = [_textView sizeThatFits:_textView.frame.size];
heightConstraint.constant = sizeThatShouldFitTheContent.height;
hightconstraint是一个布局约束,通常通过IBOutlet将属性链接到故事板中创建的高度约束来设置。
再加上这个惊人的答案,2014年,如果你:
[self.textView sizeToFit];
只有iPhone6+有不同的表现:
只有6+(不是5s或6),它确实添加了“一个更多的空白行”到UITextView。“RL解决方案”完美地解决了这个问题:
CGRect _f = self.mainPostText.frame;
_f.size.height = self.mainPostText.contentSize.height;
self.mainPostText.frame = _f;
它修复了6+上的“额外行”问题。
使用UITextViewDelegate是最简单的方法:
func textViewDidChange(_ textView: UITextView) {
textView.sizeToFit()
textviewHeight.constant = textView.contentSize.height
}