[SWIFT] To display multiple lines with UILabel

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)

Try

Install UI Label

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. ラベル置いただけ.png

Try to make a long sentence

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. ラベル伸ばす.png

** Eh ... ** It's nicely omitted ...

There was an item to set the number of lines

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. ラベル完成.png

Summary

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. storyboard項目.png

Recommended Posts

To display multiple lines with UILabel
Assignment to multiple variables with the ternary operator
How to save to multiple tables with one input
How to search multiple columns with gem ransack
How to create multiple pull-down menus with ActiveHash
Try to display hello world with spring + gradle
Easy to display hello world with Rails + Docker
How to change the action with multiple submit buttons
8 Corresponds to multiple arguments
[Rails] How to search by multiple values ​​with LIKE
Procedure to link httpd and Tomcat9 with CentOS8.3 (multiple ProxyPass)
Connect to multiple MySQL instances with SSL enabled in JDBC
I tried to be able to pass multiple objects with Ractor
Java to play with Function
Save and display multiple images
[Java] How to display Wingdings
How to number (number) with html.erb
How to update with activerecord-import
Connect to DB with Java
Connect to MySQL 8 with Java
Use multiple databases with Rails 6.0
Knowledge required to display tweets
Seasonal display with Java switch
Connect to oracle with eclipse!
Setting to start multiple units at the same time with Vagrant
Source to display character array with numberPicker in Android studio (Java)