Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
String extension to find the layout size of a String with specified attributes.
extension String {
func size(withAttributes attrs: [String:AnyObject], constrainedTo box: NSSize) -> NSRect {
let storage = NSTextStorage(string: self)
let container = NSTextContainer(containerSize: NSSize(width: box.width, height: box.height))
let layout = NSLayoutManager()
layout.addTextContainer(container)
storage.addLayoutManager(layout)
storage.addAttributes(attrs, range: NSMakeRange(0, storage.length))
container.lineFragmentPadding = 0.0
let _ = layout.glyphRangeForTextContainer(container)
return layout.usedRectForTextContainer(container)
}
}
@plumhead

This comment has been minimized.

Copy link
Owner Author

commented Sep 15, 2015

Use as follows

  • let s = "This is a string for testing the size with attributes and constrained to string extension."
  • let size = s.size(withAttributes: [NSFontAttributeName:NSFont.systemFontOfSize(20)], constrainedTo: NSSize(width: 60, height: Int.max))
@plumhead

This comment has been minimized.

Copy link
Owner Author

commented Sep 21, 2015

Obviously you could extend to pass a function in to configure the text container rather than simply setting a default lineFragmentPaddding as the example shows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.