The bold `<b>` tag and the Italic `<i>` tag do not apply to custom font families
I got a string htmlwith <b>and tags <i>.
inputString = @"<b> Sample bold text </b> Normal Text <i> sample italic </i>";
And below the method returns the attribute text for entering the html string.
+(NSAttributedString *) returnRichTextForString:(NSString *) inputString {
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithData:[inputString dataUsingEncoding:NSUTF8StringEncoding] options:@{
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)
}
documentAttributes:nil
error:nil];
return attributedString;
}
Then custom font size, family and pass it to the method described above.
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;
But, <b>and <i>do not apply on the label!
It works great with the default system font! Any clue why it fails in case of custom fonts? What should I include to receive
boldoritalic
+4
2 answers
. , , 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 -
line-heighthtmlminimumLineHeight. , 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;
}
+1