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

前端之家收集整理的这篇文章主要介绍了将自定义字体应用于从HTML字符串转换的归因字符串前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用UITextView,其中我显示NSAttributedString.我从服务器获取 HTML字符串.我使用以下代码HTML转换为NSAttributedString.
NSMutableAttributedString *attrib = [[NSMutableAttributedString alloc]initWithData:[strHTML dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} documentAttributes:nil error:nil];

要申请字体,我尝试下面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"];

使用两个代码字体更改成功.但是不应用粗体和斜体下划线在NSAttributedString中应用.

我参考以下链接.

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

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

任何帮助将被贴上!!!

解决方法

最后我得到了答案.您可以检查拉姆答案( 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];

谢谢.也许会帮助你

原文链接:https://www.f2er.com/html/223802.html

猜你在找的HTML相关文章