Anthony Anthony - 1 month ago 16
iOS Question

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?

Answer

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.