• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
UILabel HTML to attributed string conversion: odd font choice for <em> text
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

UILabel HTML to attributed string conversion: odd font choice for <em> text


  • Subject: UILabel HTML to attributed string conversion: odd font choice for <em> text
  • From: Steve Christensen <email@hidden>
  • Date: Tue, 17 Dec 2013 10:44:50 -0800

I have formatted text delivered to the app as HTML that I am converting to a NSAttributedString and then assigning to a UILabel like this:

NSDictionary* options =
@{
	NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding),
	NSDefaultAttributesDocumentAttribute:
		@{
			NSFontAttributeName:			label.font,
			NSForegroundColorAttributeName:		label.textColor,
			NSStrikethroughStyleAttributeName:	@1,
			NSUnderlineStyleAttributeName:		@(NSUnderlineStyleSingle)
		},
	NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType
};

attributedString = [[NSAttributedString alloc] initWithData:data options:options documentAttributes:NULL error:&error];

label.attributedText = attributedString;


The UILabel has its font set to Avenir Next Medium, which the code above uses as the base font for the attributed string. The HTML is of the form:

<p>Blah blah blah <em>blah blah blah</em> blah blah blah</p>

What I'm seeing visually is that the normal paragraph text is fine but that the text inside the <em> tag is nearly unreadable. I added code to dump out the attribute runs and found that the starting paragraph text is what's expected:

NSFont = "<UICTFont: 0x14c51680> font-family: \"AvenirNext-Medium\"; font-weight: normal; font-style: normal; font-size: 16.00pt";

but that the text inside the <em> tag uses Avenir Next UltraLight Italic:

NSFont = "<UICTFont: 0x14c51ee0> font-family: \"AvenirNext-UltraLightItalic\"; font-weight: normal; font-style: italic; font-size: 16.00pt";

This seems odd to me since there's a perfectly fine Avenir Next Medium Italic available. Is this an expected font choice for italic text given the setup above and, more importantly, is there a way for me to end up with an attributed string that's using the italic version of the base font short of manually walking each of the attribute runs and "correcting" the font?

Thanks,
Steve


_______________________________________________

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


  • Follow-Ups:
    • Re: UILabel HTML to attributed string conversion: odd font choice for <em> text
      • From: Jens Alfke <email@hidden>
  • Prev by Date: AutoLayout constraint priorities
  • Next by Date: Re: Best solution for game loop on OSX?
  • Previous by thread: AutoLayout constraint priorities
  • Next by thread: Re: UILabel HTML to attributed string conversion: odd font choice for <em> text
  • Index(es):
    • Date
    • Thread