After starting to develop an iOS app, I struggled because I didn't know it, so I'll leave a memorandum so I don't forget it.
TL;DR
Set numberOfLines
to 0
. (that's all)
Create a simple app that displays the Label. Place the UILabel on the storyboard and write the details in code.
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var testLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
setupLabel()
}
func setupLabel() {
testLabel.text = "It's a label"
testLabel.backgroundColor = UIColor(red: 0, green: 0, blue: 1, alpha: 0.3)
}
}
(Add a light background color so that the UI Label itself is easy to see)
Like this.
Try to make the text displayed on the Label one or more lines. If this Label is left as it is, the height is obviously not enough, so keep it high enough.
A little tweak to the code.
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var testLabel: UILabel!
///Added Constraint to adjust height
@IBOutlet weak var testLabelHeight: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
setupLabel()
}
func setupLabel() {
testLabel.text = "It will be a very long sentence. It will be a very long sentence. It will be a very long sentence. It will be a very long sentence."
testLabel.backgroundColor = UIColor(red: 0, green: 0, blue: 1, alpha: 0.3)
///To display multiple lines
testLabelHeight.constant = 80
}
}
That is this.
** Eh ... ** It's nicely omitted ...
No matter how high it is, it fits exactly in one line.
When I looked it up crying, I found that I could set the number of lines
.
Add this to the previous setupLabel ()
.
///Line number adjustment
testLabel.numberOfLines = 0
Then, it will be fine and it will be as intended.
By default, the number of lines is 1
, so if you want the specified number of lines, enter that number.
It doesn't matter how many lines you have, just put 0
to display them all.
(I was just wondering because I thought it would display everything clearly ...)
By the way, this item is also set on the storyboard.
Recommended Posts