将自定义字体应用于从HTML字符串转换的属性字符串

Chi*_*ani 12 html nsattributedstring ios ios7

UITextView在其中使用我显示NSAttributedString.我从服务器获得HTML字符串.我使用下面的代码将HTML转换为NSAttributedString.

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

要应用字体,我尝试了以下2个代码.

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

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

[attrib addAttribute:NSFontAttributeName
          value:myFont
          range:NSMakeRange(0, attrib.length)];
Run Code Online (Sandbox Code Playgroud)

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

NSString *strHTML = [NSString stringWithFormat:@"<span style=\"font-family: myFont; font-size: 12\">%@</span>",@"my string"];
Run Code Online (Sandbox Code Playgroud)

使用两个代码字体已成功更改.但Bold和Italic未应用仅Underline应用于NSAttributedString.

我参考下面的链接.

  1. iOS 7使用HTML字符串作为NSAttributedString并设置字体?
  2. 从html创建nsattributedstring时,ios7字体大小会发生变化
  3. 如何将HTML的CSS添加到NSAttributedString?

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

任何帮助都会得到满足!

Chi*_*ani 15

最后我得到了答案.您可以检查larmes答案(从用户键入的属性字符串中查找属性),或者您只需删除旧字体属性并使用以下代码应用新字体.

 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];
Run Code Online (Sandbox Code Playgroud)

谢谢.也许它会帮助你.