boundingRectWithSize and the size height parameter
boundingRectWithSize and the size height parameter
- Subject: boundingRectWithSize and the size height parameter
- From: brodhage <email@hidden>
- Date: Sat, 15 Aug 2009 00:52:33 +0200
Hello,
it seems that the size height parameter of the function
boundingRectWithSize does not work?
Apple states that the size parameter can be zero or the size of the
rectangle to draw in.
If the defaultLineHeightForFont is 30 and I set the size height to a
value (e.g. 100) I would e.g. expect something like 90. But I get
something like 120 for a text which needs 4 lines.
I tested it under MacOS X 10.4 and 10.5.
Any ideas?
mDescription is a long text (up to 20 lines) but I need to draw e.g.
only 3 lines (or 4 lines - this depends on the space left on screen)
------ First example -------
NSFont *mDescrFont = [[NSFontManager sharedFontManager]
fontWithFamily:@"Arial Narrow" traits:0 weight:5 size:descrFontSize];
NSDictionary *mDescrFontAttributes = [[NSDictionary alloc]
initWithObjectsAndKeys:
.....
mDescrFont, NSFontAttributeName,
nil];
NSAttributedString *entry = [[[NSAttributedString alloc]
initWithString:mDescription attributes:mDescrFontAttributes]
autorelease];
NSStringDrawingOptions options =
NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingOneShot |
NSStringDrawingDisableScreenFontSubstitution;
NSSize s = [entry boundingRectWithSize:NSMakeSize(500, 100)
options:options].size;
s is always the size the text needs - not the size height which fits
into it.
------ Second example -------
NSTextStorage *textStorage = [[[NSTextStorage alloc]
initWithString:mDescription] autorelease];
NSTextContainer *textContainer = [[[NSTextContainer alloc]
initWithContainerSize:NSMakeSize(width, top - 5)] autorelease];
NSLayoutManager *layoutManager = [[[NSLayoutManager alloc] init]
autorelease];
[layoutManager addTextContainer:textContainer];
[textStorage addLayoutManager:layoutManager];
[textStorage addAttributes:mDescrFontAttributes range:NSMakeRange(0,
[textStorage length])];
[textContainer setLineFragmentPadding:0.0];
[layoutManager glyphRangeForTextContainer:textContainer];
NSSize s = [layoutManager usedRectForTextContainer:textContainer].size;
Here s is also not what I would expect.
Cheers
M. Brodhage
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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