Skip to content

Instantly share code, notes, and snippets.

@reeichert
Last active May 16, 2019 22:09
Show Gist options
  • Save reeichert/965799c7677a9df2e6270a326d3ae5a3 to your computer and use it in GitHub Desktop.
Save reeichert/965799c7677a9df2e6270a326d3ae5a3 to your computer and use it in GitHub Desktop.
heightWithConstrainedWidth
extension String {
func heightWithConstrainedWidth(width: CGFloat, font: UIFont) -> CGFloat {
let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
let boundingBox = self.boundingRect(with: constraintRect,
options: .usesLineFragmentOrigin,
attributes: [NSAttributedString.Key.font: font],
context: nil)
return boundingBox.height
}
}
@reeichert
Copy link
Author

  • Updated to Swift 5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment