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 UILabel
s, 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.
To set this property, simply assign a new integer to it:
label.numberOfLines = 2
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"):
label.numberOfLines = 0
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.*
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: