Draw text with in a rect with given width
- by Forchita
I'm trying to draw text on NSView subclass. I'm using an NSTextStorage, an NSLayoutManager, and an NSTextContainer. I followed Apple's doc and here's the method I use to calculate the necessary height for each text:
NSSize newSize= NSMakeSize(width, 0.0);
NSLayoutManager* layoutManager= [[textStorage layoutManagers] objectAtIndex:0];
NSTextContainer* textContainer= [[layoutManager textContainers] objectAtIndex:0];
[textContainer setContainerSize:newSize];
[textStorage addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, [textStorage length])];
[textContainer setLineFragmentPadding:0.0];
[layoutManager glyphRangeForTextContainer:textContainer];
return [layoutManager usedRectForTextContainer:textContainer].size.height;
The only problem is that I don't know how to draw in an NSRect with the layout manager.
Can you help me please?
Thanks in advance!