4

I have a text with HTML format and I want to use this text in my iOS app. I'm using NSAttributedString to maintain the properties the text has via the HTML tags (such as <strong></strong>). The thing is, [NSAttributedString initWithData:options:documentAttributes:error:] converts the text with the default font as Times New Roman. I tried using an NSMutableAttributedString to change the font to [UIFont systemFontOfSize:], but it destroys the properties it has via the HTML tags. How can I work around this? Is there a way to change the default font?

EDIT: For example, the text I'm trying to parse is as follows:

"Activities are typically <strong>directed at a group of people</strong> rather than individuals"

The code below parses it into an HTML text with Times New Roman:

NSMutableAttributedString* itemWithStyle = [[NSMutableAttributedString alloc] initWithData:[text dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding) documentAttributes:nil error:nil];

Output:

Activities are typically directed at a group of people rather than individuals

What it looks like when I print it:

Activities are typically {
    NSColor = "UIDeviceRGBColorSpace 0 0 0 1";
    NSFont = "<UICTFont: 0xf1b34e0> font-family: \"Times New Roman\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
    NSKern = 0;
    NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (null), Lists (null), BaseWritingDirection 0, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0";
    NSStrokeColor = "UIDeviceRGBColorSpace 0 0 0 1";
    NSStrokeWidth = 0;
}directed at a group of people{
    NSColor = "UIDeviceRGBColorSpace 0 0 0 1";
    NSFont = "<UICTFont: 0x9a8aa10> font-family: \"TimesNewRomanPS-BoldMT\"; font-weight: bold; font-style: normal; font-size: 12.00pt";
    NSKern = 0;
    NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (null), Lists (null), BaseWritingDirection 0, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0";
    NSStrokeColor = "UIDeviceRGBColorSpace 0 0 0 1";
    NSStrokeWidth = 0;
} rather than individuals{
    NSColor = "UIDeviceRGBColorSpace 0 0 0 1";
    NSFont = "<UICTFont: 0xf1b34e0> font-family: \"Times New Roman\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
    NSKern = 0;
    NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (null), Lists (null), BaseWritingDirection 0, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0";
    NSStrokeColor = "UIDeviceRGBColorSpace 0 0 0 1";
    NSStrokeWidth = 0;
}

When I add the following piece of code, the text loses its attributes.

[itemWithStyle addAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:16]} range:NSMakeRange(0, itemWithStyle.length)];
halileohalilei
  • 2,220
  • 2
  • 25
  • 52
  • I don't get what your problem is. Are you trying to convert HTML formatted text into attributed string by stripping the HTML tags but keeping attributes? – Rafał Sroka May 02 '14 at 13:18
  • Could you show us you code? Exemple, how did you tried changing the font? And could you log the attributedString, before and after? – Larme May 02 '14 at 13:20
  • I added the code I wrote and its outputs. – halileohalilei May 02 '14 at 13:26
  • Could you log the attributedString? Not the output and how it's rendered, how it's logged/interpreted? – Larme May 02 '14 at 13:30
  • Ok. I'd suggest you get inspired with: http://stackoverflow.com/questions/23153156/find-attributes-from-attributed-string-that-user-typed/23153221#23153221 As you see the log, you'll see that the "bold" part is inside the font, so your font change will erase it. – Larme May 02 '14 at 13:42

1 Answers1

0

i had the same problem and solved with this:

NSMutableString * content = [[NSMutableString alloc] 
initWithString:@"<div style='font-size:15px; font-family: HelveticaNeue'>"];
[content appendString:self.article.content];
[content appendString:@"</div>"];

not sure if there is a "better" way to do it, but this did the trick for me

rob180
  • 901
  • 1
  • 9
  • 29