我已经花了3个小时来解决这个问题,但是有人知道为什么吗?请帮我!
下面的代码将图像添加为属性字符串的附件,
UIImage *img = [UIImage imageNamed:imgName];
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = img;
NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] init];
[attrStr appendAttributedString:attrStringWithImage];
然后我将attrStr分配给UITextView的attributedText,在iOS 7中效果很好,但在iOS 6中不显示图像,因为iOS 6,我听说UITextView支持attributedText,对于iOS 6,我应该做些其他事情吗?
PS:在iOS 6中,如果我将attrStr分配给UILabel的attributedText,则会显示出来,UITextView有什么特殊之处?
最佳答案
+ (NSAttributedString *)attributedStringWithAttachment:(NSTextAttachment *)attachment
在iOS 7.0和更高版本中可用。因此,在iOS 6和更低版本的iOS中,您可以使用UIWebView或第三个库在Label或TextView中显示图像。
关于nsattributedstring - 在iOS 6中将附件图像添加到属性字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23044441/