JS代码格式化富文本

  • (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 = 5
    MZ_RATE;
    [str addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, str.length)];
    return str;
    }

你可能感兴趣的:(JS代码格式化富文本)