粗体“<b>”标签和斜体“<i>”标签不适用于自定义字体系列
Bold `<b>` tag and Italic `<i>` tag not applied for custom font family
我在 html
字符串下方找到了 <b>
和 <i>
标签。
inputString = @"<b> Sample bold text </b> Normal Text <i> sample italic </i>";
下面的方法将return属性文本输入html字符串。
+(NSAttributedString *) returnRichTextForString:(NSString *) inputString {
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithData:[inputString dataUsingEncoding:NSUTF8StringEncoding] options:@{
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)
}
documentAttributes:nil
error:nil];
return attributedString;
}
然后自定义字体大小,family并传递给上面的方法。
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>
没有应用在标签上!
It works fine with default system font! Any clue why it fails in the
case of custom fonts ? What should I include to get bold
or italic
?
您应该尝试使用系统字体,以排除您的自定义字体未正确添加到项目中的可能性。
我曾经遇到过同样的问题,但有标签。
This answer 帮我解决了。也许这对你有帮助。
编辑:
在 how to add custom fonts to iOS app
上找到了一篇非常好的文章
用我自己的 hack 解决了这个问题。在跳转到解决方案之前,我在 iOS 中阐明了新添加的自定义字体的效果。也就是说,如果该字体系列带有现成的粗体/斜体等属性,则会导致上述问题。如果没有,你将面临这样的问题。
解决方案:
在 +(NSAttributedString *) returnRichTextForString:(NSString *) inputString
方法中,添加以下行以覆盖现有的 NSAttributedString 属性。
[attributedString beginEditing];
[attributedString enumerateAttribute:NSParagraphStyleAttributeName inRange:NSMakeRange(0, attributedString.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
if (value) {
NSMutableParagraphStyle *myStyle = (NSMutableParagraphStyle *)value;
//Hack for bold effect to custom fonts
if (myStyle.minimumLineHeight == 101) {
[myStyle setMinimumLineHeight:0];
[attributedString addAttribute:NSStrokeWidthAttributeName
value:[NSNumber numberWithFloat:-3.0] range:range];
}
//Hack for italic/skew effect to custom fonts
if (myStyle.minimumLineHeight == 99) {
[myStyle setMinimumLineHeight:0];
[attributedString addAttribute:NSObliquenessAttributeName
value:[NSNumber numberWithFloat:0.30] range:range];
}
}
}];
[attributedString endEditing];
下面更新了 inputString
中的标记
<style type='text/css'> b { line-height:101px;} i {line-height:99px;} </style>
因此,在上行之后的最终标记,
inputString = @"<b> Sample bold text </b> Normal Text <i> sample italic </i><style type='text/css'> b { line-height:101px;} i {line-height:99px;} </style>";
这里,
Trick is to identify bold and italic tags based on line-height
value in html and comparing it with minimumLineHeight
attribute of
paragraph style. i.e for example if line-height 101 then it will be
bold and 99 then consider it as italic.
更新:
当 和 都应用于同一文本时,这会失败。为了克服这个问题,我在这里分享了稍微修改过的逻辑。保持相同的逻辑来识别斜体和粗体我在 NSAttributedstring 的结束编辑属性之前添加了以下行,
//Hack for bold effect.
[attributedString enumerateAttribute:NSStrikethroughStyleAttributeName inRange:NSMakeRange(0, attributedString.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
if (value) {
[attributedString addAttribute:NSStrokeWidthAttributeName
value:[NSNumber numberWithFloat:-3.0] range:range];
[attributedString addAttribute:NSStrikethroughStyleAttributeName
value:[NSNumber numberWithFloat:0.0] range:range];
}
}];
并在 html、
b { text-decoration:line-through;}
我的完整方法如下:
+(NSAttributedString *) returnRichTextForString:(NSString *) inputString {
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithData:[inputString dataUsingEncoding:NSUTF8StringEncoding] options:@{
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)
}
documentAttributes:nil
error:nil];
[attributedString beginEditing];
//Hack for italic/skew effect to custom fonts
[attributedString enumerateAttribute:NSParagraphStyleAttributeName inRange:NSMakeRange(0, attributedString.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
if (value) {
NSMutableParagraphStyle *myStyle = (NSMutableParagraphStyle *)value;
if (myStyle.minimumLineHeight == 99) {
[myStyle setMinimumLineHeight:0];
[attributedString addAttribute:NSObliquenessAttributeName
value:[NSNumber numberWithFloat:0.30] range:range];
}
}
}];
//Hack for bold effect.
[attributedString enumerateAttribute:NSStrikethroughStyleAttributeName inRange:NSMakeRange(0, attributedString.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
if (value) {
[attributedString addAttribute:NSStrokeWidthAttributeName
value:[NSNumber numberWithFloat:-3.0] range:range];
[attributedString addAttribute:NSStrikethroughStyleAttributeName
value:[NSNumber numberWithFloat:0.0] range:range];
}
}];
[attributedString endEditing];
return attributedString;
}
我在 html
字符串下方找到了 <b>
和 <i>
标签。
inputString = @"<b> Sample bold text </b> Normal Text <i> sample italic </i>";
下面的方法将return属性文本输入html字符串。
+(NSAttributedString *) returnRichTextForString:(NSString *) inputString {
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithData:[inputString dataUsingEncoding:NSUTF8StringEncoding] options:@{
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)
}
documentAttributes:nil
error:nil];
return attributedString;
}
然后自定义字体大小,family并传递给上面的方法。
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>
没有应用在标签上!
It works fine with default system font! Any clue why it fails in the case of custom fonts ? What should I include to get
bold
oritalic
?
您应该尝试使用系统字体,以排除您的自定义字体未正确添加到项目中的可能性。
我曾经遇到过同样的问题,但有标签。
This answer 帮我解决了。也许这对你有帮助。
编辑:
在 how to add custom fonts to iOS app
上找到了一篇非常好的文章用我自己的 hack 解决了这个问题。在跳转到解决方案之前,我在 iOS 中阐明了新添加的自定义字体的效果。也就是说,如果该字体系列带有现成的粗体/斜体等属性,则会导致上述问题。如果没有,你将面临这样的问题。
解决方案:
在 +(NSAttributedString *) returnRichTextForString:(NSString *) inputString
方法中,添加以下行以覆盖现有的 NSAttributedString 属性。
[attributedString beginEditing];
[attributedString enumerateAttribute:NSParagraphStyleAttributeName inRange:NSMakeRange(0, attributedString.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
if (value) {
NSMutableParagraphStyle *myStyle = (NSMutableParagraphStyle *)value;
//Hack for bold effect to custom fonts
if (myStyle.minimumLineHeight == 101) {
[myStyle setMinimumLineHeight:0];
[attributedString addAttribute:NSStrokeWidthAttributeName
value:[NSNumber numberWithFloat:-3.0] range:range];
}
//Hack for italic/skew effect to custom fonts
if (myStyle.minimumLineHeight == 99) {
[myStyle setMinimumLineHeight:0];
[attributedString addAttribute:NSObliquenessAttributeName
value:[NSNumber numberWithFloat:0.30] range:range];
}
}
}];
[attributedString endEditing];
下面更新了 inputString
<style type='text/css'> b { line-height:101px;} i {line-height:99px;} </style>
因此,在上行之后的最终标记,
inputString = @"<b> Sample bold text </b> Normal Text <i> sample italic </i><style type='text/css'> b { line-height:101px;} i {line-height:99px;} </style>";
这里,
Trick is to identify bold and italic tags based on
line-height
value in html and comparing it withminimumLineHeight
attribute of paragraph style. i.e for example if line-height 101 then it will be bold and 99 then consider it as italic.
更新: 当 和 都应用于同一文本时,这会失败。为了克服这个问题,我在这里分享了稍微修改过的逻辑。保持相同的逻辑来识别斜体和粗体我在 NSAttributedstring 的结束编辑属性之前添加了以下行,
//Hack for bold effect.
[attributedString enumerateAttribute:NSStrikethroughStyleAttributeName inRange:NSMakeRange(0, attributedString.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
if (value) {
[attributedString addAttribute:NSStrokeWidthAttributeName
value:[NSNumber numberWithFloat:-3.0] range:range];
[attributedString addAttribute:NSStrikethroughStyleAttributeName
value:[NSNumber numberWithFloat:0.0] range:range];
}
}];
并在 html、
b { text-decoration:line-through;}
我的完整方法如下:
+(NSAttributedString *) returnRichTextForString:(NSString *) inputString {
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithData:[inputString dataUsingEncoding:NSUTF8StringEncoding] options:@{
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)
}
documentAttributes:nil
error:nil];
[attributedString beginEditing];
//Hack for italic/skew effect to custom fonts
[attributedString enumerateAttribute:NSParagraphStyleAttributeName inRange:NSMakeRange(0, attributedString.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
if (value) {
NSMutableParagraphStyle *myStyle = (NSMutableParagraphStyle *)value;
if (myStyle.minimumLineHeight == 99) {
[myStyle setMinimumLineHeight:0];
[attributedString addAttribute:NSObliquenessAttributeName
value:[NSNumber numberWithFloat:0.30] range:range];
}
}
}];
//Hack for bold effect.
[attributedString enumerateAttribute:NSStrikethroughStyleAttributeName inRange:NSMakeRange(0, attributedString.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
if (value) {
[attributedString addAttribute:NSStrokeWidthAttributeName
value:[NSNumber numberWithFloat:-3.0] range:range];
[attributedString addAttribute:NSStrikethroughStyleAttributeName
value:[NSNumber numberWithFloat:0.0] range:range];
}
}];
[attributedString endEditing];
return attributedString;
}