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 you show multiple lines of text in a UILabel using Swift?
I think you mean UITextView
.
Change your text to a UILabel
and hook it up as an IBOutlet
and then add the following code:
myLabel.numberOfLines = 0
Example:
@IBOutlet weak var myLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
myLabel.text = "This is where all of your text goes..."
myLabel.numberOfLines = 0
}
Set your constraints as normal and this should display the UILabel
on multiple lines dependent on how much text you have displayed.
How to do multiline UILabel in ios?
Set adjustsFontSizeToFitWidth
to NO and numberOfLines
to 0.
numberOfLines Docs
This property controls the maximum number of lines to use in order to
fit the label’s text into its bounding rectangle. The default value
for this property is 1. To remove any maximum limit, and use as many
lines as needed, set the value of this property to 0.If you constrain your text using this property, any text that does not
fit within the maximum number of lines and inside the bounding
rectangle of the label is truncated using the appropriate line break
mode.When the receiver is resized using the sizeToFit method, resizing
takes into account the value stored in this property. For example, if
this property is set to 3, the sizeToFit method resizes the receiver
so that it is big enough to display three lines of text.
You may additionally want to specify the lineBreakMode
unless the default UILineBreakModeWordWrap
is suitable for what you want.
Autoresize multiline UILabel in Swift
Below code will keep the frame size
and adjust the font size
according with direction label content
.
let backgroundView = UIView(frame: CGRect(x: 5, y: UINavigationController().navigationBar.frame.height + UIApplication.shared.statusBarFrame.height, width: UIScreen.main.bounds.width - 10, height: UIScreen.main.bounds.width - 100))
let direction = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
direction.backgroundColor = UIColor.green
direction.numberOfLines = 0
direction.textAlignment = .center
direction.font = UIFont.boldSystemFont(ofSize: 40)
direction.adjustsFontForContentSizeCategory = true
direction.adjustsFontSizeToFitWidth = true
direction.text = "This is some multiline label with a background colour" // Set or Initiate random function for your array here.
backgroundView.backgroundColor = UIColor.red
view.addSubview(backgroundView)
backgroundView.addSubview(direction)
Timer.scheduledTimer(timeInterval: 10.0, target: self, selector: #selector(random), userInfo: nil, repeats: true)
direction.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint(item: direction,
attribute: .leading,
relatedBy: .equal,
toItem: backgroundView,
attribute: .leadingMargin,
multiplier: 1.0,
constant: 0.0).isActive = true
NSLayoutConstraint(item: direction,
attribute: .trailing,
relatedBy: .equal,
toItem: backgroundView,
attribute: .trailingMargin,
multiplier: 1.0,
constant: 0.0).isActive = true
NSLayoutConstraint(item: direction,
attribute: .top,
relatedBy: .equal,
toItem: backgroundView,
attribute: .topMargin,
multiplier: 1.0,
constant: 0.0).isActive = true
NSLayoutConstraint(item: direction,
attribute: .bottom,
relatedBy: .equal,
toItem: backgroundView,
attribute: .bottomMargin,
multiplier: 1.0,
constant: 0.0).isActive = true
}
func random(sender: Timer) {
//Place your random func code here.
}
Output:
Related Topics
Why Is the Tab Bar Disappearing
Swift - Json Error: the Data Couldn'T Be Read Because It Isn'T in the Correct Format
Redirect to Application If Installed, Otherwise to App Store
How to Add Spacing Between Uitableviewcell
Update Badge With Push Notification While App in Background
Force View Controller to Reload to Refresh Uiappearance Changes
React-Native, How to Get File-Asset Image Absolute Path
Ios: How to Make a Shadow for Uiview on 4 Side (Top,Right,Bottom and Left)
Download Array of Images as Byte Array and Convert to Images
Firebase Custom Event Parameters Not Visible in Console
Is [Uiscreen Mainscreen].Bounds.Size Becoming Orientation-Dependent in Ios8
How to Use Scnetworkreachability in Swift
Change User Agent in Uiwebview
Iphone 6 Plus Resolution Confusion: Xcode or Apple'S Website? For Development