Skip to content

Instantly share code, notes, and snippets.

@catqbat
Created May 4, 2017 15:02
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 catqbat/c6b81967b5d5e92502075adb27962862 to your computer and use it in GitHub Desktop.
Save catqbat/c6b81967b5d5e92502075adb27962862 to your computer and use it in GitHub Desktop.
extension String
{
func width(withConstrainedHeight height: CGFloat, font: UIFont) -> CGFloat
{
let constraintRect = CGSize(width: .greatestFiniteMagnitude, height: height);
let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
return boundingBox.width;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment