如何将滚动功能添加到UILabel
在Xcode中,我创建了一个UILabel,它将根据我放置的文本行数自动调整大小。
但我不希望UILabel的高度超过某个限制(在我的例子中为240),代码如下:
In Xcode, I created a UILabel which will autoresize depending on how many lines of text i put on it. But I don't want the UILabel's height to exceed a certain limit (240 in my example), the code goes like this:
NSString *text = @"imagine this is a huge wall of text\n\n\n"
UILabel *myLabel = [[UILabel alloc] init];
[myLabel setNumberOfLines:0];
CGSize labelSize = [text sizeWithFont:myLabel.font constrainedToSize:CGSizeMake(280, 240) lineBreakMode:myLabel.lineBreakMode];
myLabel.frame = CGRectMake(0, 0, 280, labelSize.height);
当我的文字大约在10-15行内时,这种方法很好。
但是如果我输入40行文字,额外的文字行将超出我的UILabel并被切断。
This works fine when my text is within about 10-15 lines. But if I put in something like 40 lines of text, the extra lines of text will go beyond my UILabel and get cut off.
我该如何添加myLabel的滚动功能,以便myLabel的最大高度仍为240,我可以简单地向下滚动查看myLabel中那些额外的文本行吗?
how can I add a scroll function to myLabel so that myLabel will still have a maximum height of 240, and I can simply scroll down to view those extra lines of text in myLabel??
使用 UITextView (参考)。
它的目的就是做到这一点。禁用编辑,你会得到一个可滚动的标签。
It's designed to do exactly that. Disable editing, and you get a scrollable label.