Skip to content

Instantly share code, notes, and snippets.

@sanllier
Last active March 21, 2018 17:17
Show Gist options
  • Select an option

  • Save sanllier/172ff41c5f2dd7682e0b1bee9e7a082b to your computer and use it in GitHub Desktop.

Select an option

Save sanllier/172ff41c5f2dd7682e0b1bee9e7a082b to your computer and use it in GitHub Desktop.
extension NSAttributedString {
func size(forWidth width: CGFloat) -> CGSize {
return boundingRect(
with: CGSize(width: width, height: CGFloat.greatestFiniteMagnitude),
options: [.usesLineFragmentOrigin, .usesFontLeading],
context: nil
).size
}
}
class ASLabel: UIView {
override var intrinsicContentSize: CGSize {
        return attributedText?.size(forWidth: bounds.width) ?? .zero
    }
    override func layoutSubviews() {
        super.layoutSubviews()
if savedBounds == nil || savedBounds != bounds {
        invalidateIntrinsicContentSize()
}
savedBounds = bounds
    }
private var savedBounds: CGRect?
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment