Skip to content

Instantly share code, notes, and snippets.

@JohannMG
Created December 22, 2016 20:32
Show Gist options
  • Select an option

  • Save JohannMG/1d5f321f2e2eca9046d450c27f6e90ff to your computer and use it in GitHub Desktop.

Select an option

Save JohannMG/1d5f321f2e2eca9046d450c27f6e90ff to your computer and use it in GitHub Desktop.
Get height for constrained width string.
extension String {
func heightWithConstrainedWidth(_ width: CGFloat, font: UIFont) -> CGFloat {
let constraintRect = CGSize(width: width, height: CGFloat.greatestFiniteMagnitude)
let boundingBox = (self as NSString).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