ios - 在保持HTML属性的同时编辑NSMutableAttributedString

时间:2014-05-02 12:32:46

标签: html ios nsattributedstring

我有HTML格式的文字,我想在我的iOS应用中使用此文字。我使用NSAttributedString通过HTML标记维护文本的属性(例如<strong></strong>)。问题是,[NSAttributedString initWithData:options:documentAttributes:error:]将文本转换为默认字体Times New Roman。我尝试使用NSMutableAttributedString将字体更改为[UIFont systemFontOfSize:],但它会通过HTML标记破坏它所具有的属性。我该如何解决这个问题?有没有办法更改默认字体?

修改 例如,我试图解析的文本如下:

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

下面的代码使用Times New Roman将其解析为HTML文本:

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

输出:

  

活动通常针对一组人   人而非个人

打印时的样子:

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;
}

当我添加以下代码时,文本将丢失其属性。

[itemWithStyle addAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:16]} range:NSMakeRange(0, itemWithStyle.length)];

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题并解决了这个问题:

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

不确定是否有“更好”的方法来做到这一点,但这对我来说很有把握

相关问题