如何将滚动功能添加到UILabel [英] how to add a scroll function to a UILabel

查看:241
本文介绍了如何将滚动功能添加到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.

这篇关于如何将滚动功能添加到UILabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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