Apply Custom font to Attributed string Which Converts from HTML String

喜夏-厌秋 提交于 2019-12-17 11:45:50

问题


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 !!!


回答1:


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.



来源:https://stackoverflow.com/questions/25401742/apply-custom-font-to-attributed-string-which-converts-from-html-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!