By selecting a Label in a StoryBoard, I can select Line Break
to be Word Wrap
and change number of lines to be more than 1. How can I do that Programmatically in Swift?
To add line breaks in the text we'll use \n character in the string we want to assign to the label.
Label is a user interface item in SwiftUI which enables you to display a combination of an image (icon, SF Symbol or other) and a text label in a single UI element.
You can do this to set it programmatically
label.lineBreakMode = NSLineBreakMode.ByWordWrapping label.numberOfLines = 3
Swift 3/4
label.lineBreakMode = .byWordWrapping label.numberOfLines = 3
If you want the label to have multiple lines, do this:
var myLabel:UILabel = UILabel(frame: CGRectMake(7, 200, 370, 100)) myLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping myLabel.numberOfLines = 0 //'0' means infinite number of lines
Do remember to increase the height in "CGRectMake(7, 200, 370, 100)" <-- This
Otherwise the label won't be able to take the multiple lines of text.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With