Skip to content

Instantly share code, notes, and snippets.

@aybekckaya
Created June 26, 2018 00:18
Show Gist options
  • Save aybekckaya/928f11492f750ef6b5b38b1f8dcbab4c to your computer and use it in GitHub Desktop.
Save aybekckaya/928f11492f750ef6b5b38b1f8dcbab4c to your computer and use it in GitHub Desktop.
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: [NSAttributedStringKey.font: font], context: nil)
return ceil(boundingBox.height)
}
func width(withConstrainedHeight height: CGFloat, font: UIFont) -> CGFloat {
let constraintRect = CGSize(width: .greatestFiniteMagnitude, height: height)
let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font: font], context: nil)
return ceil(boundingBox.width)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment