What's wrong with this font thing?
What's wrong with this font thing?
- Subject: What's wrong with this font thing?
- From: Randall Meadows <email@hidden>
- Date: Wed, 29 Oct 2008 16:45:27 -0600
OK, I have to assume that I'm doing something really wrong here, but
man, I sure can't see it. Maybe my understanding is out of whack,
maybe some kind soul will show me the light... (Sorry for the length,
I'm trying to include all relevant information up front.)
I am attempting to replicate an API call that is available on another
platform, but not on Mac OS X. The call in question takes a string, a
font, a width, and reduces the font size (if necessary) theoretically
applied to that string such that when drawn, that string would occupy
no more than the specified width (down to a mimimum font size also
specified), and then returns the adjusted font size as well as the
NSSize that the string would occupy if it were to be drawn with that
font at that adjusted size.
Here is my version of that routine:
- (NSSize)sizeWithFont:(NSFont *)font
minFontSize:(CGFloat)minFontSize
actualFontSize:(CGFloat *)actualFontSize
forWidth:(CGFloat)width
{
CGFloat pointSize = [font pointSize];
NSDictionary *attrs = [NSDictionary dictionaryWithObject:font
forKey:NSFontAttributeName];
NSSize thisSize = [self sizeWithAttributes:attrs];
if (width > 0) {
while (pointSize >= minFontSize) {
attrs = [NSDictionary dictionaryWithObject:[NSFont
fontWithName:[font fontName]
size:pointSize]
forKey:NSFontAttributeName];
thisSize = [self sizeWithAttributes:attrs];
if (thisSize.width <= width) {
break;
}
pointSize -= 1.0;
}
}
if (actualFontSize != NULL) {
*actualFontSize = (pointSize > minFontSize) ? pointSize :
minFontSize;
}
return thisSize;
}
The font in question at this particular juncture is Arial Bold (NSFont
= "Arial-BoldMT 16.00 pt. P [] (0x16274640) fobj=0x16262bc0,
spc=4.45"; says gdb when I 'po font'). The font is originally
retrieved by doing "[NSFont fontWithName:@"Arial-BoldMT" size:16.0]".
If I start out at a size 20, here are the resulting sizes for a given
string:
At 20, height = 23
At 19, height = 22
At 18, height = 21
At 17, height = 20
At 16, height = 18
At 15, height = 21
At 14, height = 20
At 13, height = 19
At 12, height = 18
I forced that routine to start at 20 for debugging purposes; the
actual font sent in is of size 16, the minimum is 12, and in fact, at
12 the string fits into the specified width.
However, when I draw the string at the reduced size using -
drawInRect:withAttributes:, the baseline of the string moves; I wanted
to adjust for that by using the difference in heights from drawing it
big versus drawing it reduced, but there is no difference in heights
between 16 and 12. That boggles my mind.
Why does the height suddenly jump UP going from 16 down to 15 points?
_______________________________________________
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