Html 粗体`<;b>;`标签和斜体`<;我>;`标记未应用于自定义字体系列

Html 粗体`<;b>;`标签和斜体`<;我>;`标记未应用于自定义字体系列,html,css,objective-c,iphone,nsattributedstring,Html,Css,Objective C,Iphone,Nsattributedstring,我在下面看到了带有和标记的html字符串 inputString = @"<b> Sample bold text </b> Normal Text <i> sample italic </i>"; 然后自定义字体大小、字体系列并将其传递到上述方法中 NSString * strigAfterFontWrapper = [NSString stringWithFormat:@"<style type='text/css'> body

我在下面看到了带有
标记的
html
字符串

inputString = @"<b> Sample bold text </b> Normal Text <i> sample italic </i>";
然后自定义字体大小、字体系列并将其传递到上述方法中

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;
NSString*strigAfterFontWrapper=[NSString stringWithFormat:@“body{font size:%fpx}%@”,fontSize,customFontFamily,inputString];
label.numberOfLines=0;
NSAttributedString*attributedstring=[NTUtilities returnRichTextForString:strigAfterFontWrapper];
label.attributedText=attributedstring;
但是,
不应用于标签上

它可以与默认的系统字体配合使用!有没有线索说明它为什么会失败 自定义字体的情况?要获得
粗体
斜体
?


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

我曾经有过同样的问题,但是有标签

帮我解决了。也许这对你有帮助

编辑:


在iOS应用程序上找到一篇非常好的文章

用我自己的黑客解决了这个问题。在跳转到解决方案之前,我澄清了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>
在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;
}

如果不包装CSS,会发生什么。。系统字体可以使用吗?strigAfterFontWrapper的外观如何?请注意,HTML to NSAttributedString方法不会管理所有HTML代码。
stringAfterFontWrapper
例如,它看起来像是
body{font size:19.000000px}普通字符串示例。粗体字符串示例。斜体字符串示例。
@GaneshSomani:你说得对,使用系统字体(默认)时效果很好。但不使用随project上载的自定义字体。我完全忘了试用系统字体。谢谢你注意到我:-)@GaneshSomani:我在项目中使用的自定义字体很少是粗体、古怪、罗切斯特常规的。这个解决方案对你有用吗?我放弃投票是因为这个问题。但不幸的是,这并不能解决我的问题!
//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];
    }

}];
 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;
}