Re: "Orphaned" words not reported by NSString -boundingRectWithSize...
Re: "Orphaned" words not reported by NSString -boundingRectWithSize...
- Subject: Re: "Orphaned" words not reported by NSString -boundingRectWithSize...
- From: Aki Inoue <email@hidden>
- Date: Fri, 25 May 2007 11:18:56 -0700
The code shown should do exactly what you're trying to do, and I
cannot reproduce it with a simple sample project.
So, I'm not sure.
On thing to note if you're rendering the text using NSCell.
NSCell as paddings on both sides of text when center-aligned.
If that's the case, you might be measuring with width slightly wider
than the one used at the rendering time.
Aki
I've made a window where messages ( just strings ) stack from top to
bottom. The window is fixed width, so if a message is too long it
word wraps. Finally, since I show several messages, I have to keep
track of how tall each is so I can lay it out correctly.
Now, 99% of the time this works. But when the text is just long
enough that a single word wraps to the next line, the reported
height for the string doesn't show that line, just the lines up to
it. I can better show this with code:
First, here's how I set up the string attributes:
NSMutableParagraphStyle *pstyle = [[NSMutableParagraphStyle alloc]
init];
[pstyle setParagraphStyle: [NSParagraphStyle defaultParagraphStyle]];
[pstyle setAlignment: NSCenterTextAlignment];
[pstyle setLineBreakMode: NSLineBreakByWordWrapping];
_textDisplayAttributes = [[NSDictionary dictionaryWithObjectsAndKeys:
[NSFont messageFontOfSize: [NSFont smallSystemFontSize]],
NSFontAttributeName,
[NSColor whiteColor], NSForegroundColorAttributeName,
pstyle, NSParagraphStyleAttributeName,
nil] retain];
And here's the method preferredHeight which returns the height
needed for the string to display properly:
- (float) preferredHeight
{
NSRect br = [_message boundingRectWithSize: NSMakeSize( _width,
0 ) options:NSStringDrawingUsesLineFragmentOrigin attributes:
_textDisplayAttributes];
return br.size.height + (2.0 * _vPad);
}
I have no idea why an orphaned word wouldn't be reported. Am I doing
something bone-headed here?
Thanks,
email@hidden
"obviously, you're not a golfer"
-- the Dude
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden