Skip to content

Instantly share code, notes, and snippets.

@sanghapark
Created March 31, 2017 01:50
Show Gist options
  • Save sanghapark/96d94fd05c4767d1d2b9e23461667be9 to your computer and use it in GitHub Desktop.
Save sanghapark/96d94fd05c4767d1d2b9e23461667be9 to your computer and use it in GitHub Desktop.
[iOS] figure out UILabel height based on a given string
extension String {
func height(withConstrainedWidth width: CGFloat, font: UIFont) -> CGFloat {
let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
return boundingBox.height
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment