Skip to content

Instantly share code, notes, and snippets.

@amosavian
Created December 31, 2016 10:19
Show Gist options
  • Save amosavian/909ec5a19823dd1b07b7996a6b63cde7 to your computer and use it in GitHub Desktop.
Save amosavian/909ec5a19823dd1b07b7996a6b63cde7 to your computer and use it in GitHub Desktop.
extension String {
func height(_ width: CGFloat, font: UIFont, lineBreakMode: NSLineBreakMode?) -> CGFloat {
var attrib: [String: AnyObject] = [NSFontAttributeName: font]
if lineBreakMode != nil {
let paragraphStyle = NSMutableParagraphStyle();
paragraphStyle.lineBreakMode = lineBreakMode!;
attrib.updateValue(paragraphStyle, forKey: NSParagraphStyleAttributeName);
}
let size = CGSize(width: width, height: CGFloat(DBL_MAX));
return ceil((self as NSString).boundingRect(with: size, options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes:attrib, context: nil).height)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment