I have a UIButton with a variable titleLabel text. What I'm trying to accomplish (with AutoLayout) is that the button grows to fit the title, even when the title will need more then one line. So it has to grow in width first, and when the width reaches it's limit, it has to grow in height to fit the title. Like scenario C in the image below:
First of all: I accomplished my goal. Thanks to this and another post I subclassed UIButton and the button now works as I want it to. This is my UIButton code:
class JvBstretchableButton: UIButton {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.titleLabel?.numberOfLines = 0
self.titleLabel?.lineBreakMode = .ByWordWrapping
}
override func intrinsicContentSize() -> CGSize {
return (self.titleLabel?.intrinsicContentSize())!
}
override func layoutSubviews() {
super.layoutSubviews()
/*
Set the preferredMaxLayoutWidth of the titleLabel to the width of the superview minus
two times the known constant value for the leading and trailing button constraints
(which is 10 each). I'm looking for another way to do this. I shouldn't have to hardcode
this right? Autolayout knows the width of the surrounding view and all the relevant constraint
constants. So it should be able to figure out the preferredMaxLayoutWidth itself.
*/
self.titleLabel?.preferredMaxLayoutWidth = superview!.frame.width - 20
super.layoutSubviews()
}
}
However, I have the strong feeling that I'm missing something and that there must be an easier way to do this. That brings me to my questions:
A) Is there really the need for a UIButton subclass, or is there an alternative, easier approach?
B) In case we can't prevent subclassing UIButton: isn't there a way to let AutoLayout figure out what will be the maximum width of the button? I now have to manually pass the frame of the superview and 'hardcode' the constants of the button constraints.