The UILabel class implements a read-only text view. You can use this class to draw one or multiple lines of static text, such as those you might use to identify other parts of your user interface. The base UILabel class provides support for both simple and complex styling of the label text. You can also control over aspects of appearance, such as whether the label uses a shadow or draws with a highlight. If needed, you can customize the appearance of your text further by subclassing.
UILabel.numberOfLines: Int // get or set the maximum number of lines the label can have. 0 is unlimited
UILabel.text: String? // get or set the text the label displays
UILabel.textColor: UIColor! // get or set the color of the text on the label
UILabel.tintColor: UIColor! // get or set the tint color of the label
UILabel.attributedText: NSAttributedString? // get or set the attributed text of the label
UILabel.font: UIFont! // get or set the font of the text on the label
UILabel.textAlignment: NSTextAlignment // get or set the alignment of the text
UILabels are views which can be used to display one or many lines of text. It contains multiple ways of stylizing text, such as shadows, text colors, and fonts.
UILabels can also display Attributed Strings, which is text + inline markup to apply styles to portions of the text.
UILabel does not conform to the UIAppearance protocol, so you cannot use UIAppearance proxy methods to customise appearance of UILabels. See this discussion for more.
After the label has been created, be sure to set the dimensions via Auto Layout. Xcode will display errors if it is done improperly.
With Interface Builder
You also use Interface Builder to add a UILabel to your Storyboard or .xib file by dragging a Label from the Object Library panel and dropping it into a view in the canvas:
Instead of specifying a frame (position and size) for a UILabel programmatically, a Storyboard or a .xib lets you use Auto Layout to add constraints to the control.
In order to access this label created from storyboard or xib create an IBOutlet of this label.
Linking Between Interface Builder and View Controller
Once you have added a UILabel to your Storyboard or .xib the file you can link it to your code by pressing Control ⌃ and then dragging the mouse between the UILabel to your ViewController, or you could drag to the code while right clicking for it to have the same effect.
In the properties dialog, you can set the name of UILabel, and set it as strong or weak. For more information about strong and weak, see this,
The other way is to make the outlet programmatically as follows:
Add shadows to text
Auto-size label to fit text
This example shows how a label's width can automatically resize when the text content changes.
Pin the left and top edges
Just use auto layout to add constraints to pin the left and top sides of the label.
Don't add constraints for the width and height. Labels have an intrinsic size based on their text content.
No need to set sizeToFit when using auto layout. The complete code for the example project is here:
This method can also be used to correctly space multiple labels horizontally as in this example.
If you want your label to line wrap then set the number of lines to 0 in IB and add myLabel.preferredMaxLayoutWidth = 150 // or whatever in code. (The button is also pinned to the bottom of the label so that it will move down when the label height increased.)
Calculate Content Bounds (for i.e. dynamic cell heights)
A common use case for wanting to calculate the frame a label will take up is for sizing table view cells appropriately. The recommended way of doing this is using the NSString method boundingRectWithSize:options:attributes:context:.
options takes String drawing options:
NSStringDrawingUsesLineFragmentOrigin should be used for labels with multiple lines
NSStringDrawingTruncatesLastVisibleLine should be added using the | operator if there are a maximum number of lines
attributes is an NSDictionary of attributes that effect attributed strings (full list: Apple Docs) but the factors that effect height include:
NSFontAttributeName: Very important, the size and font family is a critical part of the label's displayed size.
For customizing how the text is displayed. This includes line spacing, text alignment, truncation style, and a few other options. If you did not explicitly change any of these values you should not have to worry about this much, but may be important if you toggled some values on IB.
context should be nil since the primary NSStringDrawingContext use case is for allowing font to resize to fit a specified rect, which shouldn't be the case if we're calculating a dynamic height.
Conversely, if you do have a set maximum number of lines you will first need calculate the height of a single line to make sure we don't get a value taller than the allowed size:
Changing Text in an Existing Label
Changing the text of an existing UILabel can be done by accessing and modifying the text property of the UILabel. This can be done directly using String literals or indirectly using variables.
Setting the text with String literals
Setting the text with a variable
NOTE: In most cases, it is better to use a UIButton instead of making a UILabel you can tap on. Only use this example, if you are sure, that you don't want to use a UIButton for some reason.
Enable user interaction
The key to create a clickable UILabel is to enable user interaction.
Setting "userInteractionEnabled" in storyboard's attributes inspector
Instead of using code, you can select the UILabel inside the storyboard and check the option:
Dynamic label frame from unknown text length
Sometimes we have to resize a UILabel based on dynamic content where the text length is unknown. In this example, width of the UILabel is fixed at 280 points and the height is infinite, lets say 9999. Estimating the frame with respect to the text style and maximumLabelSize.
Get UILabel's size strictly based on its text and font
NSString provides method boundingRectWithSize which can be used to predict the resulting CGSize of a UILabel based on its text and font without the need of creating a UILabel
Create Label and label Height constraint outlet. Add below code where you will asign text to label.
Note: "40" is the space of left and right side of screen.
Highlighted and Highlighted Text Color
Label Attributed Text
01. Underline Text :- Single/Double Line , Strike Through :- Single/Double Line
Select the Label and change the label type Plain to Attributed
Click the label text and Right click
Then click Font -> Show Fonts
Then font view will show up and click underline button to make text underline or click strikethrough button to make the text strikethrough.And select single line or double line.
Finally click enter and label will be shown underline or strikethrough according to your selection.
02. Add text shaddow/background blur effects
Get the Font view as the above described and click the effects button.
If you don't See the preview click the show image in settings
Finally change shaddow and offset according to your preferences.
This can also be set in the attributes inspector of a UILabel:
Word Wrapping - wrapping occurs at word boundaries, unless the word itself doesn’t fit on a single line
Char Wrapping - wrapping occurs before the first character that doesn’t fit
Clipping - lines are simply not drawn past the edge of the text container
Truncating Head - the line is displayed so that the end fits in the container and the missing text at the beginning of the line is indicated by an ellipsis glyph
Truncating Tail - the line is displayed so that the beginning fits in the container and the missing text at the end of the line is indicated by an ellipsis glyph
Truncating Middle - the line is displayed so that the beginning and end fit in the container and the missing text in the middle is indicated by an ellipsis glyph
Number of Lines
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:
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"):
If the label has a height constraint, the constraint will be respected. In this case, label.numberOfLines = 0 may not work as expected.
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:
Change the default font's size
Use a specific font weight
Use a Dynamic Type text style.
The font and point size will be based on the user's preferred reading size.
Use a different font altogether
Override font size
A way to set the font size without knowing the font family is to use the font property of the UILabel.