将自定义字体应用于从 HTML 字符串转换的属性字符串 [英] Apply Custom font to Attributed string Which Converts from HTML String
问题描述
我使用了 UITextView
,其中显示了 NSAttributedString
.我从服务器得到 HTML 字符串.我使用以下代码将 HTML 转换为 NSAttributedString.
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];
要应用字体,我尝试了以下 2 个代码.
To Apply font i tried below 2 codes.
- 应用属性
- 自定义字符串
要应用属性,我使用以下代码.
To Apply Attribute i use below code.
[attrib addAttribute:NSFontAttributeName
value:myFont
range:NSMakeRange(0, attrib.length)];
对于自定义字符串,我首先在 NSString
下创建,然后在 NSAttributedString
中转换
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"];
使用两种代码 字体更改成功.但粗体和斜体 不只应用 下划线 在 NSAttributedString 中应用.
Using both code Font changed successfully. but Bold and Italic not applied only Underline is applied in NSAttributedString.
我参考以下链接.
- iOS 7 使用将 HTML 字符串作为 NSAttributedString 并设置字体?
- 从html创建nsattributedstring时ios7字体大小变化一个>
- 如何将 html 的 CSS 添加到 NSAttributedString?
对于链接 3,我应该从服务器端应用字体和标签,然后检索 HTML 字符串吗?
for link 3 should i apply fonts and tags from server side and then retrieve HTML String?
任何帮助都会被感谢!!!
Any Help would be appriciated !!!
推荐答案
我终于得到了答案.您可以检查 larmes answer(从属性字符串中查找属性用户输入),或者您可以简单地删除旧字体属性并使用以下代码应用新字体.
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 字符串转换的属性字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!