Re: Font Height and -[NSString sizeWithAttributes:]
Re: Font Height and -[NSString sizeWithAttributes:]
- Subject: Re: Font Height and -[NSString sizeWithAttributes:]
- From: Quincey Morris <email@hidden>
- Date: Mon, 28 Mar 2011 12:06:43 -0700
On Mar 28, 2011, at 09:45, Thomas Clément wrote:
>> NSFont *font = [NSFont fontWithName:@"Menlo" size:11.0];
>> [@"Hello World" sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil]];
>
> This returns a height of 17.0. However when using this font (Menlo 11.0) in TextEdit (plain text document), I'm seeing a line height of 13.0.
>
> Monaco 10.0 returns a height of 14.0 which is also what I'm seeing in TextEdit.
> Helvetica 12.0 returns a height of 15.0 but I'm seeing 14.0 in TextEdit.
>
> Why these differences?
'sizeWithAttributes:' returns the bounding box, in points, of the glyphs representing the characters in the string you passed.
> How do I get programmatically the value I'm seeing in TextEdit?
There's no absolute typographic standard for the inter-baseline distance, but it's almost always derived from font metrics, not from glyph bounding boxes. Here are some of the possibilities:
-- The font size multiplied by a standard factor, like 120%. This is (or used to be, at least) what apps like Adobe Illustrator used to use, because it doesn't depend on the specific font metrics, and therefore the line spacing of a line doesn't depend on what fonts are on it, only on their sizes.
-- The font's ascender + descender + "leading". These are metrics built into the font itself, independent of the actual glyph bounds. This is probably what TextEdit is using. (What Apple calls "leading" is really called "extra leading" in the typographic world. Typographically, leading *is* the inter-baseline distance: the whole thing.) IIRC, changing fonts in TextEdit can affect the inter-baseline distance on a line-by-line basis.
-- The glyph bounding box, either of the particular line, or of all the characters in the font. You'd only use this if you can't tolerate glyph images overlapping, ever.
-- Any weird calculation that someone thought was a good idea at the time.
There's no right answer, unfortunately.
_______________________________________________
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