Re: Font Height and -[NSString sizeWithAttributes:]
Re: Font Height and -[NSString sizeWithAttributes:]
- Subject: Re: Font Height and -[NSString sizeWithAttributes:]
- From: Thomas Clement <email@hidden>
- Date: Tue, 29 Mar 2011 21:18:42 +0200
Right I think NSLayoutManager will solve my problem.
Lots of things in that class.
Thanks all!
Thomas
On 28 mars 2011, at 21:17, Aki Inoue wrote:
> The default line height used by the Cocoa Text System is based on various layout time configurations. So, the differences you're seeing is coming from the differences in layout context.
>
> The settings are all encapsulated in NSLayoutManager; hence, the method -[NSLayoutManager defaultLineHeightForFont:] gives you the information used by the layout context.
>
> Aki
>
> On 2011/03/28, at 12:06, Quincey Morris wrote:
>
>> 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
>
_______________________________________________
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