关于富文本NSMutableAttributedString

来来来 先上来一个封装的富文本函数吧,集成了图片文本色下划线等等
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px 'PingFang SC'; color: #1d9421}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #6122ae}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1d9421}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; min-height: 21.0px}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3d1d81}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #6122ae}span.s3 {font: 18.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #000000}span.s4 {font: 18.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s5 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s6 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s7 {font: 18.0px 'PingFang SC'; font-variant-ligatures: no-common-ligatures}span.s8 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s9 {font-variant-ligatures: no-common-ligatures; color: #0435ff}span.s10 {font-variant-ligatures: no-common-ligatures; color: #c32275}

  • (NSAttributedString *)attrbutedString:(NSString *)text withImage:(UIImage *)image
    {
    // 创建可变属性字符串,富文本
    NSMutableAttributedString * attrString = [[NSMutableAttributedString alloc] init];
    // 将UIImage转换为Attachement
    NSTextAttachment * attachment = [[NSTextAttachment alloc] init];
    attachment.image = image;

    // 将Attachement转变为富文本
    NSAttributedString * attachementAttr = [NSAttributedString attributedStringWithAttachment:attachment];

    // 将text转变为富文本
    NSAttributedString * textAttr = [[NSAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]}];

    // 拼接
    [attrString appendAttributedString:attachementAttr];
    [attrString appendAttributedString:textAttr];

    return [attrString copy];
    }

使用的时候注意哦p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #703daa}

Lable.attributedText = attStr;

你可能感兴趣的:(关于富文本NSMutableAttributedString)