html – 粗体“标签和斜体“标签不适用于自定义字体系列
我在< 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>不适用于标签!
解决方法
用我自己的黑客解决了这个问题.在跳转到解决方案之前,我澄清了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>"; 这里,
更新: //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; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- js replace替换所有匹配的字符串
- jquery 结合C#后台的数组对文章的关键字自动添加
- 如何点击htmlunit中的javascript按钮
- html – 尝试在Bootstrap 3中水平和垂直居中div
- Web服务 – Netbeans 6.9.1中的JAX-RPC支持
- html – 如果select标签具有visibility:hidden,
- 微信小程序 连续旋转动画(this.animation.rotat
- jquery中get,post和ajax方法的使用小结
- wcf – HttpContext.Current在我的Web服务中为空
- domain-name-system – 使用DNSSEC管理多个相等的