Number of Lines

suggest change

When you make a label and set its text to be more than a single line that it can display, it will be truncated and you will see only one line of text ending with three dots (…). This is because a property called numberOfLines is set to 1, and therefore only one line will be displayed. It is a common mistake in handling UILabels, and many people think of it as a bug, or they may use more than one label to show more than a line of text, but just by editing this property, we can tell a UILabel to accept up to the specified number of lines. For example, if this property is set to 5, the label can show 1, 2, 3, 4 or 5 lines of data.

Setting the value programmatically

To set this property, simply assign a new integer to it:

Swift

label.numberOfLines = 2

Objective-C

label.numberOfLines = 2;

Note

It is possible to set this property to 0. However, this doesn’t mean that it won’t accept any lines, instead it means that the label can have as many lines as needed (aka “Infinity”):

Swift

label.numberOfLines = 0

Objective-C

label.numberOfLines = 0;

Note

If the label has a height constraint, the constraint will be respected. In this case, label.numberOfLines = 0 may not work as expected.

Note

For a more complex multi-line text, UITextView may be a better fit.*

Setting the value in the Interface Builder

Instead of setting numberOfLines programmatically, you can use a Storyboard or a .xib and set the numberOfLines property. That way, we achieve the same results as the above code.

Like as below:

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents