nsattributedstring - 在iOS 6中将附件图像添加到属性字符串

标签 nsattributedstring nstextattachment

我已经花了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/

相关文章:

uiimage - 渲染完毕后更新NSTextAttachment的图像

cocoa - 如何在 TextView 中实现水平线?

ios - 为 UITableViewCell 的 NSTextAttachment 异步加载图像

ios - 向 UILabel 的一部分添加点击手势

ios - 在不修改其他属性的情况下替换 NSAttributedString 中的整个文本字符串

ios - 使用大标题时如何将导航标题左对齐?

cocoa - NSAttributedString 中的段落是如何定义的?

uitextview - 如何将菜单选项添加到 NSTextAttachment 弹出菜单是 UITextView?

cocoa - 如何将可点击的复选框插入 nstextview 中?

ios - 使用NSAttributedString不正确滚动UITextView