xcode 自动换行在我的 UILabel 上不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30751257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Word wrap not working on my UILabel
提问by user616076
My app uses a custom cell to display products in a tableview. The product name when used on an iPhone 5 was running off the end so I set it to wrap which doesn't work. Each cell is populated in my Customcell class using the code below, I've added a couple of lines to the brandTxt label which again does not work.
我的应用程序使用自定义单元格在 tableview 中显示产品。在 iPhone 5 上使用时,产品名称用完了,所以我将它设置为 wrap ,这不起作用。每个单元格都使用下面的代码填充到我的 Customcell 类中,我在brandTxt 标签中添加了几行,这又不起作用。
public func configure(#text: String?, placeholder: String) {
if (placeholder == "1")
{
brandItemLabel.text = text
brandItemLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
brandItemLabel.numberOfLines = 2
}
else if (placeholder == "2")
{
brandSmallLabel.text = text
}
else if (placeholder == "3")
{
uomCodeLabel.text = text
}
else if (placeholder == "4")
{
codeLabel.text = text
}
else if (placeholder == "6")
{
var image = UIImage(named: "Blank.jpg")
if let url = NSURL(string: text!) {
if let data = NSData(contentsOfURL: url) {
image = UIImage(data: data)
}
}
var imgViewLbl = UIImageView(image: image) as UIImageView
imgViewLbl.frame = CGRect(x: 10, y: 4, width: 90, height: 90)
customView.addSubview(imgViewLbl)
}
}
I've also tried using constraints but that is not working either
我也试过使用约束,但这也不起作用
I've since added the code below using preferredMaxLayoutWidth as well as setting the Preferred Width to Explicit in Size Manager and added Constraints to set the size and height as well as the position.
我已经使用 preferredMaxLayoutWidth 添加了下面的代码,并在大小管理器中将首选宽度设置为显式,并添加了约束来设置大小和高度以及位置。
public func configure(#text: String?, placeholder: String) {
if (placeholder == "1")
{
brandItemLabel.text = text
brandItemLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
brandItemLabel.numberOfLines = 2
brandItemLabel.preferredMaxLayoutWidth = 330
}
The UILabel is still not wrapping in spite of these changes
尽管进行了这些更改,但 UILabel 仍未换行
回答by i89
When you are targeting iOS 6 until 8 or higher, you have to explicitly set the preferredWidth attribute on the UILabel. You have to do it, if the label has more than one line. See also preferredMaxLayoutWidth:
当您的目标是 iOS 6 到 8 或更高版本时,您必须在 UILabel 上显式设置 preferredWidth 属性。如果标签有多于一行,则必须这样做。另见preferredMaxLayoutWidth:
So you just need to specify this value (in most cases it's good to go to the storyboard, click on the label, go to the "Size Inspector" and just click on 'explicit' next to "Preferred Width").
所以你只需要指定这个值(在大多数情况下,最好去故事板,点击标签,去“尺寸检查器”,然后点击“首选宽度”旁边的“显式”)。
回答by Purnima Naik
Checking Follow Readable Width
in Size Inspector and setting larger height, did it for me!
检查Follow Readable Width
尺寸检查器并设置更大的高度,为我做到了!