Created
December 31, 2016 10:19
-
-
Save amosavian/909ec5a19823dd1b07b7996a6b63cde7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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