自动换行不适用于UILabel [英] Word Wrap not working for UILabel

查看:56
本文介绍了自动换行不适用于UILabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用自动布局,我无法在代码中覆盖标签.我已经在IB中设置了标签属性:Lines = 0,LineBreaks = Word Wrap,但是我将高度设置为单行,因为由于选择了哪个单元格,所以确定了标签中将包含哪些文本.因此,有时标签将只有一行.

Using autolayout I can't override my label in code. I've set the labels attributes in IB: Lines = 0, LineBreaks = Word Wrap, but I have my height set to a single line because due to what cell is selected determines what text goes in the label. So sometimes the label will only have one line.

在我的viewDidLoad中:

In my viewDidLoad:

myLabel.text = @"blah, blah, blah….";
[myLabel setLineBreakMode:NSLineBreakByWordWrapping];
myLabel.numberOfLines  = 0; //have tried 1 but didn’t help
[myLabel sizeToFit];

这可用于另一个项目,但我没有使用自动版式.AutoLayout似乎会覆盖这些设置.
我什至添加了

This works on another project, but I wasn’t using AutoLayout. AutoLayout seems to override these settings.
I’ve even added

[myLabel setFrame:CGRectMake(20, 135, 280, 80); 

但这没有帮助.

推荐答案

允许标签的固有尺寸确定高度.正确的是,您需要将numberOfLines属性设置为0.由于使用的是自动版式,因此请勿调用sizeToFit,而需要设置标签的preferredMaxLayoutWidth.

Allow the intrinsic size of the label determine the height. You are correct that you need to set the numberOfLines property to 0. Since you are using AutoLayout, don't call sizeToFit and you need set the preferredMaxLayoutWidth of the label.

这篇关于自动换行不适用于UILabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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