如何在 ios 中做多行 UILabel?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7996108/
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
How to do multiline UILabel in ios?
提问by HardCode
I'm dynamically populating the title (UILabel). Sometime it's bit too long and IOS squeeze the font to fit in the width. Is there a way to do multiline with using same font size?
我正在动态填充标题 (UILabel)。有时它太长了,IOS 会挤压字体以适应宽度。有没有办法使用相同的字体大小做多行?
回答by Sam
Set adjustsFontSizeToFitWidth
to NO and numberOfLines
to 0.
设置adjustsFontSizeToFitWidth
为 NO 和numberOfLines
0。
numberOfLines Docs
numberOfLines 文档
This property controls the maximum number of lines to use in order to fit the label's text into its bounding rectangle. The default value for this property is 1. To remove any maximum limit, and use as many lines as needed, set the value of this property to 0.
If you constrain your text using this property, any text that does not fit within the maximum number of lines and inside the bounding rectangle of the label is truncated using the appropriate line break mode.
When the receiver is resized using the sizeToFit method, resizing takes into account the value stored in this property. For example, if this property is set to 3, the sizeToFit method resizes the receiver so that it is big enough to display three lines of text.
此属性控制要使用的最大行数,以使标签的文本适合其边界矩形。此属性的默认值为 1。要删除任何最大限制并根据需要使用尽可能多的行,请将此属性的值设置为 0。
如果您使用此属性限制文本,则任何不符合最大行数和标签边界矩形内的文本都将使用适当的换行符模式截断。
当使用 sizeToFit 方法调整接收器的大小时,调整大小会考虑存储在此属性中的值。例如,如果此属性设置为 3,则 sizeToFit 方法会调整接收器的大小,使其大到足以显示三行文本。
You may additionally want to specify the lineBreakMode
unless the default UILineBreakModeWordWrap
is suitable for what you want.
您可能还想指定lineBreakMode
除非默认UILineBreakModeWordWrap
值适合您想要的。
回答by Sagar Surani
Use UILabel
properties as below :
使用UILabel
属性如下:
textLabel.lineBreakMode = NSLineBreakByWordWrapping;
textLabel.numberOfLines = 0;
回答by jrturton
Yes, you set the numberOfLines
to 0, and the .lineBreakMode
to UILineBreakModeWordWrap
in code, or the equivalents if your label is defined in IB.
是的,如果您的标签是在 IB 中定义的,则您将 设置numberOfLines
为 0,将.lineBreakMode
设置UILineBreakModeWordWrap
为代码中或等效项。
回答by Waqas
I just used LineBreak mode to Truncate Tail and setNumberOfLines to 0 and its working for me. the label is of multiline now.
我只是使用 LineBreak 模式来 Truncate Tail 和 setNumberOfLines 为 0 并且它对我有用。现在标签是多行的。