Created
July 21, 2016 03:00
-
-
Save joshavant/0540b463b877f0bdecefbc44848a4501 to your computer and use it in GitHub Desktop.
UITextView Height Calculation
This file contains 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 UITextView { | |
// Note: This will trigger a text rendering! | |
func calculateViewHeightWithCurrentWidth() -> CGFloat { | |
let textWidth = self.frame.width - | |
self.textContainerInset.left - | |
self.textContainerInset.right - | |
self.textContainer.lineFragmentPadding * 2.0 - | |
self.contentInset.left - | |
self.contentInset.right | |
let maxSize = CGSize(width: textWidth, height: CGFloat.max) | |
var calculatedSize = self.attributedText.boundingRectWithSize(maxSize, | |
options: [.UsesLineFragmentOrigin, .UsesFontLeading], | |
context: nil).size | |
calculatedSize.height += self.textContainerInset.top | |
calculatedSize.height += self.textContainerInset.bottom | |
return ceil(calculatedSize.height) | |
} | |
} |
Thank you for this gist. It works flawlessly.
working for me π
Thanks god, you saved me π π π π π π π π π π π π π π π
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
extension UITextView {
// Note: This will trigger a text rendering!
func calculateViewHeightWithCurrentWidth() -> CGFloat {
let textWidth = self.frame.width -
self.textContainerInset.left -
self.textContainerInset.right -
self.textContainer.lineFragmentPadding * 2.0 -
self.contentInset.left -
self.contentInset.right
}