iOS 富文本的应用(图片与文字混编)

341 阅读1分钟
NSString *Str = [NSString stringWithFormat:@"参与喜中%@能量!",self.dic[@"backCoin"]];

 // 创建一个富文本

NSMutableAttributedString * attriStr = [[NSMutableAttributedString alloc] initWithString:Str];

 // 修改富文本中的不同文字的样式

 [attriStr addAttribute:NSForegroundColorAttributeName value:kAppThemeColor range:NSMakeRange(0,2)];

 /**
     添加图片到指定的位置
*/
 NSTextAttachment *attchImage = [[NSTextAttachment alloc] init];  

// 表情图片

attchImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:urlofstr(self.dic[@"vgoodsImg"])]];

// 设置图片大小

attchImage.bounds = CGRectMake(0, -8, 30, 30);

NSAttributedString *stringImage = [NSAttributedString attributedStringWithAttachment:attchImage];

[attriStr insertAttributedString:stringImage atIndex:2];

// 赋值
rightLabel.attributedText = attriStr;



常见的属性及说明

NSFontAttributeName  字体

NSParagraphStyleAttributeName  段落格式 

NSForegroundColorAttributeName  字体颜色

NSBackgroundColorAttributeName   背景颜色

NSStrikethroughStyleAttributeName 删除线格式

NSUnderlineStyleAttributeName      下划线格式

NSStrokeColorAttributeName        删除线颜色

NSStrokeWidthAttributeName 删除线宽度

NSShadowAttributeName  阴影