Anthony Shahine
Anthony Shahine

Reputation: 2487

swift 3 ios : UILabel

I'm trying to create a UILabel, with dynamic height

this is my code :

        label1.text = Message
        label1.textColor = UIColor.white
        let font = UIFont.systemFont(ofSize: 17.0)
        label1.font = font
        label1.textAlignment = .center
        label1.lineBreakMode = NSLineBreakMode.byWordWrapping;
        label1.numberOfLines = 0;
        label1.sizeToFit()

        label1.frame = CGRect(x: 0, y:40 , width: 270, height: label1.frame.height)

however, my label stay with one line, where my Message is about 3 lines so what could be the best solution for this problem?

Upvotes: 0

Views: 916

Answers (3)

balkaran singh
balkaran singh

Reputation: 2786

you can do it with storyboard enter image description here

label height relation to greater than or equal

Upvotes: 0

d.felber
d.felber

Reputation: 5418

The best solution would be using constraints.

Either directly in the InterfaceBuilder or via code.
(SnapKit would be a good and easy way to build constraints via code)

Upvotes: 1

clemens
clemens

Reputation: 17712

Try

let size = label1.sizeThatFits(CGSize(width: 270, height: 100000))
label1.frame = CGRect(x: 0, y: 40, width: 270, height: size.height)

for the last two line of your code.

Upvotes: 0

Related Questions