加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

html – 粗体“标签和斜体“标签不适用于自定义字体系列

发布时间:2020-12-14 23:27:01 所属栏目:资源 来源:网络整理
导读:我在 b下面的html字符串下面和 i标签. inputString = @"b Sample bold text /b Normal Text i sample italic /i"; 并且下面的方法将返回输入html字符串的属性文本. +(NSAttributedString *) returnRichTextForString:(NSString *) inputString { NSMutableAt
我在< b>下面的html字符串下面和< 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>不适用于标签!

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
?

解决方法

用我自己的黑客解决了这个问题.在跳转到解决方案之前,我澄清了iOS中新添加的自定义字体的效果.也就是说,如果该字体系列带有现成的Bold / Italic等属性,则会导致上述问题.如果没有,你将面临这样的问题.

方案:

在(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,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,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,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;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读