Last active
March 21, 2018 17:17
-
-
Save sanllier/172ff41c5f2dd7682e0b1bee9e7a082b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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