如何调整UITextView的宽度以适应其内容而不包装? [英] How to adjust UITextView's width to fit its content without wrapping?

查看:171
本文介绍了如何调整UITextView的宽度以适应其内容而不包装?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调整UITextView的大小以适应其内容的高度可以这样实现:

Resizing UITextView to fit height of its content can be achieved like this:

CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
_textView.frame = frame;

是否有类似宽度的东西,没有包装内容文本?

Is there something similar to fit width, without wrapping the content text?

推荐答案

1) 您可以使用 NSString UIKit Additions 来计算文本的大小(并调整相应的UITextView的大小)。您可能会看到一个示例此处

1) You can use the NSString UIKit Additions to compute the size taken by the text (and adjust the size of your UITextView accordingly). You may seen an example here.

例如,如果如果在一行上渲染,你需要知道你的 NSString 所采用的CGSize,使用 CGSize sz = [_textView.text sizeWithFont:_textView。字体] 然后在给定此大小值的情况下调整您的帧。

For example, if you need to know the CGSize that is taken by your NSString if rendered on a single line, use CGSize sz = [_textView.text sizeWithFont:_textView.font] then adjust your frame given this size value.

如果您需要知道文本占用的大小多行,如果文本达到给定宽度,则包装它,使用 sizeWithFont:constrainedToSize:lineBreakMode:代替等等。

If you need to know the size taken by your text if rendered on multiple lines, wrapping it if the text reaches a given width, use sizeWithFont:constrainedToSize:lineBreakMode: instead, etc.

2) 您可能还对 sizeThatFits感兴趣: sizeToFit UIView的方法
第一个返回CGSize(适合传入参数的给定CGSize),以便您的UITextView可以显示所有文本。第二个实际上是调整大小,为你调整帧。

2) You may also be interested in the sizeThatFits: and sizeToFit methods of UIView. The first one returns the CGSize (that fits in the given CGSize passed in parameter) so that your UITextView can display all your text. The second actually do the resizing, adjusting the frame for you.

这篇关于如何调整UITextView的宽度以适应其内容而不包装?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆