9

I have spent 3 hours on this but just can't solve it, anybody knows why? please help me!

The code below added an image as an attachment to an attributed string,

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];

Then I assigned attrStr to a UITextView's attributedText, this works great in iOS 7 but in iOS 6 the image is not displayed, I heard attributedText is supported for UITextView since iOS 6, is there anything different should I do for iOS 6?

PS: in iOS 6, if I assign attrStr to UILabel's attributedText, it is displayed, what's the special for UITextView?

Fuli
  • 263
  • 2
  • 4
  • 11
  • The issue is that `attributedStringWithAttachment` is available in iOS7.0 and later. It should crash. `NSAttributedString` was indeed introduced in iOS6, but in iOS7 there were plenty of things added. – Larme Apr 21 '14 at 17:19
  • 1
    @Larme : `NSAttributedString` was available since iOS 3.2+, not iOS 6. – Sepehrom Jun 02 '14 at 13:59

2 Answers2

1

+ (NSAttributedString *)attributedStringWithAttachment:(NSTextAttachment *)attachment is available in iOS 7.0 and later. So in iOS 6 and earlier version of iOS, you may use UIWebView or third library to display image in a Label or TextView.

mskfisher
  • 3,028
  • 3
  • 31
  • 47
Ting Wu
  • 11
  • 1
0

The NSTextAttachment class available in iOS 7.0 and later,so it is not work for you in iOS6 and earlier version.