Skip to content

Instantly share code, notes, and snippets.

@aheze
Created February 10, 2020 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aheze/d152ca76eabdc485308bc1b7068313f7 to your computer and use it in GitHub Desktop.
Save aheze/d152ca76eabdc485308bc1b7068313f7 to your computer and use it in GitHub Desktop.
extension String {
func heightWithConstrainedWidth(width: CGFloat, font: UIFont) -> CGFloat {
let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
let boundingBox = self.boundingRect(with: constraintRect, options: [.usesLineFragmentOrigin, .usesFontLeading], attributes: [NSAttributedString.Key.font: font], context: nil)
return boundingBox.height
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment