Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save saurabh-selldo/6f037e3d0d056a98e349a4923b70fadd to your computer and use it in GitHub Desktop.
Save saurabh-selldo/6f037e3d0d056a98e349a4923b70fadd to your computer and use it in GitHub Desktop.
UITextView Height Calculation
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)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment