iOS multiline label in Interface builder
You can use numberOfLines
property which defines maximum number of lines a label can have. By default, it's 1
. Setting it to 0
means the label will have unlimited lines.
You can do it in code:
textLabel.numberOfLines = 5 // for example
Or in Interface Builder:
Multiple lines of text in UILabel
Set the line break mode to word-wrapping and the number of lines to 0
:
// Swift
textLabel.lineBreakMode = .byWordWrapping
textLabel.numberOfLines = 0
// Objective-C
textLabel.lineBreakMode = NSLineBreakByWordWrapping;
textLabel.numberOfLines = 0;
// C# (Xamarin.iOS)
textLabel.LineBreakMode = UILineBreakMode.WordWrap;
textLabel.Lines = 0;
Restored old answer (for reference and devs willing to support iOS below 6.0):
textLabel.lineBreakMode = UILineBreakModeWordWrap;
textLabel.numberOfLines = 0;
On the side: both enum values yield to 0
anyway.
How do I set adaptive multiline UILabel text?
I kind of got things working by adding auto layout constraints:
But I am not happy with this. Took a lot of trial and error and couldn't understand why this worked.
Also I had to add to use titleLabel.numberOfLines = 0
in my ViewController
Multiline label in UIStackView
The correct answer is here:
https://stackoverflow.com/a/43110590/566360
- Embed the
UILabel
inside aUIView
(Editor -> Embed In -> View) - Use constraints to fit the
UILabel
to theUIView
(for example, trailing space, top space, and leading space to superview constraints)
The UIStackView
will stretch out the UIView
to fit properly, and the UIView
will constrain the UILabel
to multiple lines.
UIButton with multi-line titleLabel in InterfaceBuilder
Code:
To allow multiple line you can use:
button.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
button.titleLabel.textAlignment = UITextAlignmentCenter;
[button setTitle: @"Line1\nLine2" forState: UIControlStateNormal];
In iOS 6, UILineBreakModeWordWrap
and UITextAlignmentCenter
are deprecated, so use:
button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
button.titleLabel.textAlignment = NSTextAlignmentCenter;
Interface Builder:
- In interface builder select UIButton
- on the right side Utilities pane under Attributes Inspector, you'll see an option for Line Break
- Choose Word Wrap
How do you add multi-line text to a UIButton?
For iOS 6 and above, use the following to allow multiple lines:
button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
// you probably want to center it
button.titleLabel.textAlignment = NSTextAlignmentCenter; // if you want to
[button setTitle: @"Line1\nLine2" forState: UIControlStateNormal];
For iOS 5 and below use the following to allow multiple lines:
button.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
// you probably want to center it
button.titleLabel.textAlignment = UITextAlignmentCenter;
[button setTitle: @"Line1\nLine2" forState: UIControlStateNormal];
2017, for iOS9 forward,
generally, just do these two things:
- choose "Attributed Text"
- on the "Line Break" popup select "Word Wrap"
UIStackView and Multiline Labels?
This really seems to be a bug.
A workaround that works for me is embedding the multiline label in a view and leaving it there.
That fixes the layout on the Storyboard editor and also works in the simulator.
A weird thing is that if I have several multiline labels on the same StackView I only have to embed one of them in a UIView, and then all the other multiline labels will behave properly.
Related Topics
How to Connect Multiple Buttons in a Storyboard to a Single Action
How to Parse Iso 8601 Using Nsdateformatter with Optional Milliseconds Part
Swift: Random Number for 64-Bit Integers
How to Pass Data from Child to Parent View Controller? in Swift
iOS Swift: How to Change the Font Style of a Certain Word in a String
App Updates, Nsurl, and Documents Directory
Location Access - App Is Not Asking for User Permission to Access Location - iOS 11
Tracking Mkmapview Centercoordinate While Panning
Receive Accelerometer Updates in Background Using Coremotion Framework
How to Respond to Push Notification View If App Is Already Running in the Background
Xcode - How to Drag a Component from One View to Another Without Losing Its Frame
How to Hide the Keyboard When I Press Return Key in a Uitextfield
Switching Between Text Fields on Pressing Return Key in Swift