如何在ios中进行多行UILabel?

时间:2021-08-26 14:39:38

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挤压字体以适应宽度。有没有办法使用相同的字体大小做多行?

4 个解决方案

#1


75  

Set adjustsFontSizeToFitWidth to NO and numberOfLines to 0.

将adjustsFontSizeToFitWidth设置为NO,将numberOfLines设置为0。

numberOfLines Docs

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.

此属性控制使用的最大行数,以便将标签的文本放入其边界矩形。此属性的默认值为1.要删除任何最大限​​制,并根据需要使用尽可能多的行,请将此属性的值设置为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.

使用sizeToFit方法调整接收器大小时,调整大小会考虑存储在此属性中的值。例如,如果此属性设置为3,则sizeToFit方法会调整接收器的大小,使其足够大以显示三行文本。

You may additionally want to specify the lineBreakMode unless the default UILineBreakModeWordWrap is suitable for what you want.

除非默认的UILineBreakModeWordWrap适合您的需要,否则您可能还需要指定lineBreakMode。

#2


5  

Use UILabel properties as below :

使用UILabel属性如下:

textLabel.lineBreakMode = NSLineBreakByWordWrapping;  
textLabel.numberOfLines = 0;

#3


4  

Yes, you set the numberOfLines to 0, and the .lineBreakMode to UILineBreakModeWordWrap in code, or the equivalents if your label is defined in IB.

是的,您将numberOfLines设置为0,将.lineBreakMode设置为代码中的UILineBreakModeWordWrap,或者如果您的标签在IB中定义,则等效。

#4


2  

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并且它为我工作。标签现在是多行的。

#1


75  

Set adjustsFontSizeToFitWidth to NO and numberOfLines to 0.

将adjustsFontSizeToFitWidth设置为NO,将numberOfLines设置为0。

numberOfLines Docs

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.

此属性控制使用的最大行数,以便将标签的文本放入其边界矩形。此属性的默认值为1.要删除任何最大限​​制,并根据需要使用尽可能多的行,请将此属性的值设置为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.

使用sizeToFit方法调整接收器大小时,调整大小会考虑存储在此属性中的值。例如,如果此属性设置为3,则sizeToFit方法会调整接收器的大小,使其足够大以显示三行文本。

You may additionally want to specify the lineBreakMode unless the default UILineBreakModeWordWrap is suitable for what you want.

除非默认的UILineBreakModeWordWrap适合您的需要,否则您可能还需要指定lineBreakMode。

#2


5  

Use UILabel properties as below :

使用UILabel属性如下:

textLabel.lineBreakMode = NSLineBreakByWordWrapping;  
textLabel.numberOfLines = 0;

#3


4  

Yes, you set the numberOfLines to 0, and the .lineBreakMode to UILineBreakModeWordWrap in code, or the equivalents if your label is defined in IB.

是的,您将numberOfLines设置为0,将.lineBreakMode设置为代码中的UILineBreakModeWordWrap,或者如果您的标签在IB中定义,则等效。

#4


2  

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并且它为我工作。标签现在是多行的。