Draw text with in a rect with given width
Posted
by
Forchita
on Stack Overflow
See other posts from Stack Overflow
or by Forchita
Published on 2010-12-26T22:38:57Z
Indexed on
2010/12/26
22:54 UTC
Read the original article
Hit count: 182
objective-c
|cocoa
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!
© Stack Overflow or respective owner