Reputation: 2487
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
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
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