将自定义字体应用于从HTML字符串转换的Attributed字符串 [英] Apply Custom font to Attributed string Which Converts from HTML String

查看:146
本文介绍了将自定义字体应用于从HTML字符串转换的Attributed字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了 UITextView ,其中我显示 NSAttributedString 。我从服务器获取了 HTML 字符串。我使用下面的代码将HTML转换为NSAttributedString。

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

要申请字体,我尝试了2个以下的代码。


  1. apply Attribute

  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中。 我在下面的链接中引用。


  1. iOS 7使用HTML字符串作为NSAttributedString并设置字体?

  2. 当从html创建nsattributedstring时,字体大小发生变化

  3. 如何将html的CSS添加到NSAttributedString?

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



任何帮助将被appriciated !!!

解决方案

最后,我得到了答案河你可以检查副书面答案(从属性字符串查找属性用户输入),或者你可以简单地删除旧的字体属性和使用下面的代码应用新的字体。

  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 *)值;
NSLog(@%@,oldFont.fontName);

/ * -----删除旧的字体属性----- * /
[attrib removeAttribute:NSFontAttributeName range:range];
//用新的替换你的字体。
/ * -----添加新的字体属性----- * /
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];

谢谢。也许它会帮助你。


I used UITextView in which i display NSAttributedString. i got HTML string from server . I converted HTML to NSAttributedString using below code.

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

To Apply font i tried below 2 codes.

  1. apply Attribute
  2. Custom String

To Apply Attribute i use below code.

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

For Custom String first i create below NSString and then covert in NSAttributedString

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

Using both code Font changed successfully. but Bold and Italic not applied only Underline is applied in NSAttributedString.

i refer below links.

  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?

for link 3 should i apply fonts and tags from server side and then retrieve HTML String?

Any Help would be appriciated !!!

解决方案

Finally i got answer. you can check larmes answer(Find attributes from attributed string that user typed) or you can simply remove old font attribute and apply new font using below code.

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

Thanks. Maybe it will help you.

这篇关于将自定义字体应用于从HTML字符串转换的Attributed字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆