iOS NSTextAttachment富文本
2016-07-23 本文已影响510人
屈涯
实现一段文字中,插入一个一张图片
+(NSAttributedString*)getString:(NSString*)string pictureName:(UIImage*)imageName textFount:(CGFloat)FountSize pictureSize:(CGRect)pictureRect
{
// NSTextAttach可以将图片转换为富文本内容
NSTextAttachment*attachment = [[NSTextAttachmentalloc]init];
//创建通过NSTextAttachment的富文本
//图片的富文本
attachment.image= imageName;
//文字的富文本
NSAttributedString*imageAttr = [NSAttributedStringattributedStringWithAttachment:attachment];
NSAttributedString*textAttr = [[NSAttributedStringalloc]initWithString:stringattributes:@{NSFontAttributeName: [UIFontsystemFontOfSize:FountSize]}];
NSMutableAttributedString*mutableAttr = [[NSMutableAttributedStringalloc]init];
[mutableAttrappendAttributedString:textAttr];
[mutableAttrappendAttributedString:imageAttr];
return[mutableAttrcopy];
}