NSDocumentTypeDocumentAttribute不适用于NSFontAttributeName

时间:2014-03-18 08:30:34

标签: ios nsattributedstring

故事

应用程序接收带有在CMS中编辑的html标记的字符串。应用程序接收该字符串并放入UILabel。不久前,html标签被添加到这个字符串中。显然,带有html标签的字符串在网站中看起来很好。

我进行了调查,发现我们可以使用UILabel的归因字符串。

//attributes dictionary
NSDictionary *attrs =
@{
     NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
 };

NSAttributedString* attrString = [[NSAttributedString alloc] initWithData:[textString dataUsingEncoding:NSUnicodeStringEncoding] 
                                                                  options:attrs
                                                       documentAttributes:nil 
                                                                    error:nil];

好的,现在标签已启用。但我丢失了我的字体,文字看起来像......默认的html文字!这些字符串从css中获取字体,但我收到带有html标签的字符串。只需将NSFontAttributeName: [UIFont systemFontOfSize:10.f]添加到attrs字典即可。但没有任何成功。

我使用NSMutableAttributedString进行了第二次尝试:

//attributes dictionary
NSDictionary *attrs =
@{
     NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
 };

NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithData:[textString dataUsingEncoding:NSUnicodeStringEncoding] 
                                                                  options:attrs
                                                       documentAttributes:nil 
                                                                    error:nil];
//and set font later
[attrString setAttributes:@{NSFontAttributeName:font} range:NSMakeRange(0, attrString.length)];

现在我的标签没有任何html标签,但最后一步会覆盖标签上的所有更改(粗体,斜体等)。

问题

我们如何在属性字符串中使用html标记,但为它们设置字体?

1 个答案:

答案 0 :(得分:1)

我也在寻找这个问题的解决方案。 到目前为止,我通过使用以下代码解决了这个问题。在这里,我知道HTML总是包含Times新罗马字体。但如果有人发现任何动态解决方案,请愉快地提供。

NSRange range = NSMakeRange(0, [parsedAttributedString length]);
    [parsedAttributedString enumerateAttribute:NSFontAttributeName inRange:range options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(id value, NSRange range, BOOL *stop)
    {
        UIFont* currentFont = value;
        UIFont *replacementFont = nil;

            if ([currentFont.fontName rangeOfString:@"BoldMT" options:NSLiteralSearch].location != NSNotFound)
            {
                replacementFont = [UIFont fontWithName:@"HelveticaNeue-Bold" size:14.0f];
            }
            else if ([currentFont.fontName rangeOfString:@"BoldItalicMT" options:NSLiteralSearch].location != NSNotFound)
            {
                replacementFont = [UIFont fontWithName:@"HelveticaNeue-BoldItalic" size:14.0f];
            }
            else if ([currentFont.fontName rangeOfString:@"ItalicMT" options:NSLiteralSearch].location != NSNotFound)
            {
                replacementFont = [UIFont fontWithName:@"HelveticaNeue-Italic" size:14.0f];
            }
            else
            {
                replacementFont = [UIFont fontWithName:@"HelveticaNeue-Thin" size:14.0f];
            }

            [parsedAttributedString addAttribute:NSFontAttributeName value:replacementFont range:range];
    }];