Created
July 9, 2014 16:57
-
-
Save tid-kijyun/936ccc30f902b5c95f63 to your computer and use it in GitHub Desktop.
横幅からWrappedなテキストが納まる領域の高さを求める方法 ref: http://qiita.com/_tid_/items/e6e959e4b82c7f478211
This file contains hidden or 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
func heightForStringDrawing(myString: String, myFont: NSFont, myWidth: CGFloat) -> CGFloat | |
{ | |
var textStorage = NSTextStorage(string: myString) | |
var textContainer = NSTextContainer(containerSize: NSMakeSize(myWidth, CGFloat(FLT_MAX))) | |
var layoutManager = NSLayoutManager() | |
layoutManager.addTextContainer(textContainer) | |
textStorage.addLayoutManager(layoutManager) | |
textStorage.addAttribute(NSFontAttributeName, value:myFont, range:NSMakeRange(0, textStorage.length)); | |
textContainer.lineFragmentPadding = 0.0 | |
layoutManager.glyphRangeForTextContainer(textContainer) | |
return layoutManager.usedRectForTextContainer(textContainer).size.height; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment