【问题标题】:Apply Custom font to Attributed string Which Converts from HTML String将自定义字体应用于从 HTML 字符串转换的属性字符串
【发布时间】:2014-10-13 15:14:52
【问题描述】:

我使用了UITextView,其中显示了NSAttributedString。我从服务器得到 HTML 字符串。我使用以下代码将 HTML 转换为 NSAttributedString。

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

为了应用字体,我尝试了以下 2 个代码。

  1. 应用属性
  2. 自定义字符串

要应用属性,我使用下面的代码。

[attrib addAttribute:NSFontAttributeName
          value:myFont
          range:NSMakeRange(0, attrib.length)];

对于自定义字符串,我首先在NSString 下创建,然后在NSAttributedString 中隐藏

NSString *strHTML = [NSString stringWithFormat:@"<span style=\"font-family: myFont; font-size: 12\">%@</span>",@"my string"];

使用这两个代码字体更改成功。但 Bold 和 Italic 未应用,仅 Underline 在 NSAttributedString 中应用。

我参考下面的链接。

  1. iOS 7 using an HTML string as an NSAttributedString AND setting the font?
  2. ios7 font size change when create nsattributedstring from html
  3. How to add CSS of an html to NSAttributedString?

对于链接 3,我应该从服务器端应用字体和标签,然后检索 HTML 字符串吗?

任何帮助都会得到帮助!!!

【问题讨论】:

  • 如果我没记错的话,问题在于NSFontAttributeName 内部的粗体和斜体效果。没有 NSItalicAttributeName 或 NSBoldAttributeName。因此,一种方法是枚举属性并以此启发您:stackoverflow.com/questions/23153156/…
  • @Larme 是的,我尝试了自己并做了同样的事情。:)。谢谢。

标签: html ios ios7 nsattributedstring


【解决方案1】:

我终于得到了答案。您可以检查 larmes 答案(Find attributes from attributed string that user typed),或者您可以简单地删除旧字体属性并使用以下代码应用新字体。

 NSDictionary *dictAttrib = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,  NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)};
 NSMutableAttributedString *attrib = [[NSMutableAttributedString alloc]initWithData:[yourHTMLString dataUsingEncoding:NSUTF8StringEncoding] options:dictAttrib documentAttributes:nil error:nil];
 [attrib beginEditing];
        [attrib enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, attrib.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
            if (value) {
                UIFont *oldFont = (UIFont *)value;
                NSLog(@"%@",oldFont.fontName);

                /*----- Remove old font attribute -----*/
                [attrib removeAttribute:NSFontAttributeName range:range];
                //replace your font with new.
                /*----- Add new font attribute -----*/
                if ([oldFont.fontName isEqualToString:@"TimesNewRomanPSMT"])
                    [attrib addAttribute:NSFontAttributeName value:font1 range:range];
                else if([oldFont.fontName isEqualToString:@"TimesNewRomanPS-BoldMT"])
                    [attrib addAttribute:NSFontAttributeName value:font2 range:range];
                else if([oldFont.fontName isEqualToString:@"TimesNewRomanPS-ItalicMT"])
                    [attrib addAttribute:NSFontAttributeName value:font3 range:range];
                else if([oldFont.fontName isEqualToString:@"TimesNewRomanPS-BoldItalicMT"])
                    [attrib addAttribute:NSFontAttributeName value:font4 range:range];
                else
                    [attrib addAttribute:NSFontAttributeName value:font5 range:range];
            }
        }];
[attrib endEditing];

谢谢。也许对你有帮助。

【讨论】:

    猜你喜欢
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-19
    • 2017-01-17
    • 2016-11-23
    相关资源
    最近更新 更多