- 創建NSTextAttachment的對象,用來裝在圖片
- 將NSTextAttachment對象的image屬性設置為想要使用的圖片
- 設置NSTextAttachment對象bounds大小,也就是要顯示的圖片的大小
-
用[NSAttributedString attributedStringWithAttachment:attch]方法,將圖片添加到富文本上
//創建富文本字符串 NSMutableAttributedString *mAString = [[NSMutableAttributedString alloc] initWithString:@"你好你好你好你好你好你好你你好你好你好你好你好你好你你好你好你好你好你好你好你"]; //創建圖片 NSTextAttachment *attch = [[NSTextAttachment alloc] init]; attch.image = [UIImage imageNamed:@"ding"]; attch.bounds = CGRectMake(0, -2, 16, 16); //將圖片加入富文本 NSAttributedString *imgString = [NSAttributedString attributedStringWithAttachment:attch]; [mAString appendAttributedString:imgString]; _Alabel.attributedText = mAString;
效果圖:

原文鏈接:http://www.jianshu.com/p/5828173adc3a