【问题标题】:Bold `<b>` tag and Italic `<i>` tag not applied for custom font family粗体 `<b>` 标记和斜体 `<i>` 标记不适用于自定义字体系列
【发布时间】:2015-07-25 12:12:29
【问题描述】:

我得到低于html 字符串&lt;b&gt;&lt;i&gt; 标签。

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;

但是,&lt;b&gt;&lt;i&gt; 没有应用在标签上!

使用默认系统字体可以正常工作!任何线索为什么它在 自定义字体的情况?我应该包括什么来获得bolditalic ?

【问题讨论】:

  • 不包装 CSS 会发生什么情况。系统字体可以正常工作吗?
  • strigAfterFontWrapper 看起来像什么?请注意,HTML 到 NSAttributedString 方法并不管理所有 HTML 代码。
  • stringAfterFontWrapper 例如它看起来像这样&lt;style type='text/css'&gt; body {font-size: 19.000000px}&lt;/style&gt;&lt;font face='Comfortaa'&gt; Normal string example.&lt;b&gt;Bold string sample.&lt;/b&gt;&lt;i&gt;Italic String example.&lt;/font&gt;
  • @GaneshSomani:你说得对,它适用于使用的系统字体(默认)。但不适用于随项目上传的自定义字体。我完全忘了尝试使用系统字体! .谢谢你注意到我:-)
  • @GaneshSomani :我在项目中使用的自定义字体很少是BankGothic Bold, Eccentric, Rochester Regular..

标签: html css objective-c iphone nsattributedstring


【解决方案1】:

用我自己的 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>";

这里,

技巧是根据line-height识别粗体和斜体标签 html 中的值并将其与minimumLineHeight 属性进行比较 段落样式。即例如,如果 line-height 101 那么它将是 粗体和 99 则将其视为斜体。

更新: 当两者都应用于相同的文本时,这将失败。为了克服这个问题,在这里我分享了稍微修改的逻辑。保持相同的逻辑来识别斜体和粗体,我在结束编辑 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;
}

【讨论】:

    【解决方案2】:

    您应该尝试使用系统字体,以排除您的自定义字体未正确添加到项目中的可能性。

    我曾经遇到过同样的问题,但是标签问题。

    This answer 为我解决了这个问题。也许这对你有帮助。

    编辑:

    how to add custom fonts 上找到一篇非常好的文章到 iOS 应用

    【讨论】:

    • 这个解决方案对你有用吗?
    • 我因为问题的原因放弃了投票。但不幸的是,这并不能解决我的问题!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-07
    • 1970-01-01
    • 1970-01-01
    • 2016-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多