如何截断UITextView内容以适应缩小的尺寸? [英] How to truncate UITextView contents to fit a reduced size?
问题描述
我在解决这个问题时遇到了麻烦。
I'm having real trouble getting my head around this issue.
正如标题所示,我在iPhone应用程序的视图中有几个UITextView。我以编程方式创建它们并成功地用文本填充textview,但在某些情况下,我放在视图中的文本比我为其分配的帧占用更多空间。在这种情况下,我希望文本被截断,但我无法弄清楚如何去做。
As the title suggests, I have several UITextViews on a view in an iPhone application. I am programmatically creating them and successfully filling that textview with text, but in some cases the text I put in the view takes up more space than the frame I allocate for it. In this case I would like the text to be truncated, but I can't figure out how to do it.
我预先定义了以下常量;
I have predefined the following constants;
#define viewOriginX 20
#define viewOriginY 180
这是我的UITextView创建代码;
Here is my UITextView creation code;
textViewOne = [[UITextView alloc] initWithFrame:CGRectMake(viewOriginX, viewOriginY + 65, 280, 45];
textViewOne.delegate = self;
textViewOne.scrollEnabled = NO;
textViewOne.contentInset = UIEdgeInsetsZero;
textViewOne.font = viewFont;
textViewOne.textColor = [UIColor blackColor];
textViewOne.textAlignment = UITextAlignmentLeft;
[self.view addSubview:textViewOne];
在某些情况下,我在这里有15到20行文字,我想将其截断为2行。
In some cases I have 15 to 20 lines of text in here and I would like to truncate it to 2 lines.
任何人都可以帮我正确的方向吗?
Can anyone help me in the right direction?
提前致谢!:D
推荐答案
您可以做一个字符数。例如,如果你有这样的UITextField:
You can do a character count. For example, if you have UITextField like this:
+--------------------+
|This is a string in |
|a text view. |
+--------------------+
每行有20个字符。如果您知道该号码,则可以通过 -substringToIndex:
方法截断字符串。
you have something like 20 characters per line. If you know that number you can simply truncate your string by -substringToIndex:
method.
int maxCharacters = 40; // change it to your max
if([myString length] > maxCharacters) {
myString = [myString substringToIndex:maxCharacters];
}
你也可以考虑一下的UILabel
。由于您只需要2行文本,因此UILabel的 numberOfLines
属性可以解决您的问题。
You can also think about UILabel
. Since you need only 2 lines of text, UILabel's numberOfLines
property can solve your problem.
这篇关于如何截断UITextView内容以适应缩小的尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!