在情节提要中链接时,UILabel 的 lineBreakMode 不起作用
Posted
技术标签:
【中文标题】在情节提要中链接时,UILabel 的 lineBreakMode 不起作用【英文标题】:lineBreakMode of UILabel doesn't work when linked in the storyboard 【发布时间】:2014-12-24 09:43:52 【问题描述】:我尝试用 Xcode 为 ipad/iphone 做我的第一个应用程序。我在 ViewController.h 中创建了一个 UILabel 和一个 IBAction:
@interface ViewController : UIViewController
IBOutlet UILabel *display;
- (IBAction)ChangeText;
我将变量“display”与我在 Main.storyboard 中创建的标签和方法“ChangeText”链接到一个按钮。
标签和按钮使用自动布局放置。
标签最初包含文本“-”,当我点击按钮时,“ChangeText”方法将标签的文本更改为更长的文本:
- (IBAction)ChangeText
[display setText:@"A long text which exceeds the size of the screen I am using. A long text which exceeds the size of the screen I am using.A long text which exceeds the size of the screen I am using."];
由于文本对于屏幕来说太长了,我想分几行显示它。所以我改变了lineBreakMode和“显示”的numberOfLines:
- (IBAction)ChangeText
display.lineBreakMode = NSLineBreakByWordWrapping;
display.numberOfLines = 0;
[display setText:@"A long text which exceeds the size of the screen I am using. A long text which exceeds the size of the screen I am using.A long text which exceeds the size of the screen I am using."];
不幸的是,结果是一样的,文字只显示在一行上。
您知道我为什么以及如何克服这个问题吗?
提前致谢。
【问题讨论】:
确保您的 UILabel 有足够的空间(相应设置高度?) 在情节提要中将 UILabel 文本样式设置为“属性文本”,然后运行 @Zach:检查标签框。 @SumitGarg 我在情节提要中将文本从“Plain”更改为“Attributed”,但显示仍然相同。 @dehlen 根据自动布局的约束,标签应该有足够的空间来添加一些行。否则,由于标签是在情节提要中创建的,我不知道如何设置它的高度。 【参考方案1】:dehlen 是对的,标签没有足够的空间。
我现在不应该/可以在情节提要中手动扩展标签的大小。我还必须添加一个“垂直间距”约束,以便自动布局感到满意。
【讨论】:
以上是关于在情节提要中链接时,UILabel 的 lineBreakMode 不起作用的主要内容,如果未能解决你的问题,请参考以下文章