Swift 2:
yourLabel.text = "your very long text"
yourLabel.numberOfLines = 0
yourLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
yourLabel.frame.size.width = 200
yourLabel.frame.size.height = CGFloat(MAXFLOAT)
yourLabel.sizeToFit()
The interesting lines are sizeToFit()
in conjunction with setting a frame.size.height
to the max float, this will give room for long text, but sizeToFit()
will force it to only use the necessary, but ALWAYS call it after setting the .frame.size.height
.
I recommend setting a .backgroundColor
for debug purposes, this way you can see the frame being rendered for each case.