In interface builder, holding Command + = will resize a button to fit its text. I was wondering if this was possible to do programmatically before the
Swift:
The important thing here is when will you call the .sizeToFit()
, it should be after setting the text to display so it can read the size needed, if you later update text, then call it again.
var button = UIButton()
button.setTitle("Length of this text determines size", forState: UIControlState.Normal)
button.sizeToFit()
self.view.addSubview(button)