Skip navigation.
 
mlRe: error converting HTML into an attributed string
FROM : Douglas Davidson
DATE : Mon Nov 26 21:03:56 2007

On Nov 19, 2007, at 4:11 PM, Mark Sanvitale wrote:

> The initWithHTML method of NSAttributedString is exactly what I 
> need, and it works, for the most part.  However, I am experiencing 
> one annoying problem with the conversion, and I'm wondering if 
> anyone has any insight into it (i.e. despite my displeasure, perhaps 
> this is the way it is intended to work).
>
> The problem is that the attributed-string / rich-text that I get 
> from some HTML data always has a font size one-third bigger (rounded 
> to the nearest whole integer) than what is specified in the HTML. 
> The fact that I am seeing an exact multiple, I thought this might 
> (somehow) be on purpose.  The HTML I am asking to be converted is 
> fairly limited.  It has a bunch of tags of the form:
>
>     <ilayer style="font-family: Courier;font-size: 20pt;color: 
> rgb(51,139,213);">
>
> i.e. an inconsequential HTML tag that includes inline CSS properties 
> in order to specify the look of the text.  The problem is that the 
> font size in the CSS does not result in an equivalent size in the 
> resulting attributed string.  In this example, the resulting font 
> size is 27 (20 * 1 1/3 = 26.66666 = 27 with rounding).


Unfortunately, there are a number of cases in which the way that web 
browsers actually work differs from what the standard may specify, due 
to compatibility considerations.  CSS points were originally intended 
to be absolute units of 1/72", but the reality is otherwise.  For 
compatibility with IE, browsers assume a hard-coded ratio of 1 pt = 
4/3 px.  (Try creating a document with text in (a) 12px; (b) 12pt; (c) 
16px and opening it in any browser.)

Since CSS px units correspond most closely to Cocoa's points, font 
sizes specified in px map one-to-one, while font sizes specified in pt 
are scaled by 4/3.  At the moment there is no way to control the 4/3 
ratio between pt and px.  If this is HTML that you control, the 
recommendation is to use px instead of pt.  For example, AppKit's HTML 
generation uses px so that fonts round-trip correctly.

Douglas Davidson

Related mailsAuthorDate
mlerror converting HTML into an attributed string Mark Sanvitale Nov 20, 01:11
mlRe: error converting HTML into an attributed string Mark Sanvitale Nov 20, 01:17
mlRe: error converting HTML into an attributed string Rob Keniger Nov 20, 02:14
mlRe: error converting HTML into an attributed string Douglas Davidson Nov 26, 21:03