【发布时间】:2015-07-25 12:12:29
【问题描述】:
我得到低于html 字符串<b> 和<i> 标签。
inputString = @"<b> Sample bold text </b> Normal Text <i> sample italic </i>";
下面的方法将返回输入 html 字符串的属性文本。
+(NSAttributedString *) returnRichTextForString:(NSString *) inputString {
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithData:[inputString dataUsingEncoding:NSUTF8StringEncoding] options:@{
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)
}
documentAttributes:nil
error:nil];
return attributedString;
}
然后自定义字体大小、家族并将其传递给上述方法。
NSString * strigAfterFontWrapper = [NSString stringWithFormat:@"<style type='text/css'> body {font-size: %fpx}</style><font face='%@'>%@</font>", fontSize , customFontFamily, inputString];
label.numberOfLines = 0;
NSAttributedString *attributedstring = [NTUtilities returnRichTextForString:strigAfterFontWrapper];
label.attributedText = attributedstring;
但是,<b> 和 <i> 没有应用在标签上!
使用默认系统字体可以正常工作!任何线索为什么它在 自定义字体的情况?我应该包括什么来获得
bold或italic?
【问题讨论】:
-
不包装 CSS 会发生什么情况。系统字体可以正常工作吗?
-
strigAfterFontWrapper看起来像什么?请注意,HTML 到 NSAttributedString 方法并不管理所有 HTML 代码。 -
stringAfterFontWrapper例如它看起来像这样<style type='text/css'> body {font-size: 19.000000px}</style><font face='Comfortaa'> Normal string example.<b>Bold string sample.</b><i>Italic String example.</font> -
@GaneshSomani:你说得对,它适用于使用的系统字体(默认)。但不适用于随项目上传的自定义字体。我完全忘了尝试使用系统字体! .谢谢你注意到我:-)
-
@GaneshSomani :我在项目中使用的自定义字体很少是
BankGothic Bold, Eccentric, Rochester Regular..
标签: html css objective-c iphone nsattributedstring