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:


UILabel:
* Number of Lines

Table Of Contents
1 UILabel
12 UIView
15 UIColor
26 UIImage
28 CALayer
30 NSDate
40 iBeacon
49 NSTimer
79 NSURL
87 AWS SDK
96 NSData
101 Segues
104 EventKit
105 NSBundle
106 SiriKit
111 StoreKit
117 3D Touch
119 Keychain
122 Block
141 AirDrop
144 UISlider
145 Carthage
146 HealthKit
151 plist
157 MVVM
164 UIPhoenix
166 Simulator
168 NSArray
169 OpenGL
175 Core Data
179 MyLayout
180 UIFont
189 Security
200 Codable