4

I've successfully been using NSTextAttachments in a UITextView for some time, however only ever using the image property, like so:

NSTextAttachment *attachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
attachment.image = [UIImage imageNamed:@"someImage"];

I'm interested to try using the initWithData:ofType:, however I can't seem to find any documentation that describes what kind of NSData the NSTextAttachment class supports that can be rendered in a UITextView.

For instance, can I pass data of an NSString or some kind of UIView? Or even a PDF? Or is it limited to actually be UIImage data?

jscs
  • 63,694
  • 13
  • 151
  • 195
petehare
  • 1,874
  • 16
  • 14
  • @JoshCaswell More specifically I'm wondering what UITextView can render. Just edited the question, thanks. – petehare Dec 18 '14 at 21:44

1 Answers1

2

I guess that the data might be any arbitrary data that represents your attachment (to be saved for example in RTF format). The image property on the other side is used only to actually render the representation of the attachment. So for example when you attach a pdf file in the Mail app I think it creates an attachment object setting its data to the actual data of the pdf and setting the image to a UIImage object that holds the pdf icon and the file name. This later image might be generated directly by Mail app. So all in all, I don't think UITextView renders any attachment data neither it concerns the contents of the data property: you have to provide an image that you want to be visualized in the text view, and set that data to whatever you need.

MrTJ
  • 13,064
  • 4
  • 41
  • 63
  • It seems like NSTextAttachment invalidates it's "image" property the moment you set attachment.contents: See Apple Docs - https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/NSTextAttachment_Class_TextKit/#//apple_ref/occ/instp/NSTextAttachment/contents Even though your logic is solid, it seems like Apple thinks differently or I am absolutely dumb +) – ilia510 Oct 16 '15 at 14:47
  • Maybe they generate a new image based on the NSData you set in the contents field? – MrTJ Oct 19 '15 at 09:49
  • 1
    MrTJ, i guess so. In this case it works fine with an image, but if you want to work with another type of attachment, then NStextAttachment does not render it to generate a .image based on contents. I am now doing a subclass of NSTextAttachment to be able to add other attachments (like mail app in iOS9) and keep the contents data. I don't see other way to achieve that. – ilia510 Oct 19 '15 at 14:46