-
(NSMutableAttributedString *)fullHTMLStringExtendToMutableString:(NSString *)bodyString width:(float)textWidth font:(UIFont *)font textColor:(UIColor *)color{
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithData:[bodyString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSFontAttributeName : font } documentAttributes:nil error:nil];
[str enumerateAttribute:NSAttachmentAttributeName inRange:NSMakeRange(0, str.length) options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
if (value && [value isKindOfClass:[NSTextAttachment class]]) {
NSTextAttachment *textAttachment = value;
CGFloat width = CGRectGetWidth(textAttachment.bounds);
CGFloat height = CGRectGetHeight(textAttachment.bounds);
if (width > [UIScreen mainScreen].bounds.size.width) {// 大于屏幕宽度时,缩小bounds宽度,高度
height = textWidth / width * height;
width = textWidth;
textAttachment.bounds = CGRectMake(0, 0, width, height);
}}
}];
[str addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, str.length)];
[str addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, str.length)];NSMutableParagraphStyle style = [[NSMutableParagraphStyle alloc] init];
// style.lineSpacing = 5MZ_RATE;
[str addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, str.length)];
return str;
}
JS代码格式化富文本
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 本文所涉及的代码你可以在这里下载到https://github.com/kejinlu/CTTest,包含两个项目...