Skip to content

Instantly share code, notes, and snippets.

@tid-kijyun
Last active January 27, 2017 08:45
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 tid-kijyun/4c1b786061c1e8e8676078b44b2de98f to your computer and use it in GitHub Desktop.
Save tid-kijyun/4c1b786061c1e8e8676078b44b2de98f to your computer and use it in GitHub Desktop.
byWordWrappingな文字列に必要な高さを求めてみる
func heightForStringDrawing2(myString: String, myFont: NSFont, myWidth: CGFloat) -> CGFloat {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineBreakMode = .byWordWrapping
let attribute: [String : Any] = [
NSFontAttributeName: myFont,
NSParagraphStyleAttributeName: paragraphStyle
]
let constraintsSize = CGSize(width: myWidth, height: CGFloat(FLT_MAX))
let textSize = NSString(string: myString).boundingRect(with: constraintsSize, options: .usesLineFragmentOrigin, attributes: attribute, context: nil)
return textSize.height
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment