- (void)setNameAddSexString:(NSString * )string sex:(int)gender{
NSMutableAttributedString * attriStr = [[NSMutableAttributedString alloc] initWithString:string];
/**
添加图片到指定的位置
*/
NSTextAttachment *attchImage = [[NSTextAttachment alloc] init];
// 表情图片
attchImage.image = [UIImage imageNamed:gender == 1 ? @"man" : gender == 2 ? @"women" : @""];//man women
// 设置图片大小
attchImage.bounds = CGRectMake(pt(4), 0, pt(18), pt(18));
NSAttributedString *stringImage = [NSAttributedString attributedStringWithAttachment:attchImage];
[attriStr insertAttributedString:stringImage atIndex:string.length];
self.groupTitleLabel.attributedText = attriStr;
}
//Swift
let attri = NSMutableAttributedString.init(string: "哈哈哈哈");
let attriImage = NSTextAttachment()
attriImage.image = UIImage.init(named: "icon");
attriImage.bounds = CGRect.init(x: 0, y: 0, width: 20, height: 20);
attri.insert(NSAttributedString.init(attachment: attriImage), at: 1);
label?.attributedText = attri;