Skip to content

Instantly share code, notes, and snippets.

@isoiphone
Created July 17, 2015 18:12
Show Gist options
  • Save isoiphone/ae86813a8e8c1e3ac1da to your computer and use it in GitHub Desktop.
Save isoiphone/ae86813a8e8c1e3ac1da to your computer and use it in GitHub Desktop.
because ... :(
import Foundation
// based on: http://stackoverflow.com/questions/18118021/how-to-resize-superview-to-fit-all-subviews-with-autolayout
// required so we can have multiline labels properly resize their height based on content
class MultilineLabel: UILabel {
override func layoutSubviews() {
super.layoutSubviews()
if numberOfLines == 0 && preferredMaxLayoutWidth != CGRectGetWidth(frame) {
preferredMaxLayoutWidth = frame.size.width
setNeedsUpdateConstraints()
}
}
override func intrinsicContentSize() -> CGSize {
var s = super.intrinsicContentSize()
if numberOfLines == 0 {
// found out that sometimes intrinsicContentSize is 1pt too short!
s.height += 1
}
return s
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment