1

I have a text field to which I load the content using JSON. How would I also add images to it? For example if I were to use the image link for it.

enter image description here

This is the way I want it. I want to be able to display pictures in-between texts.

Sinan Samet
  • 6,432
  • 12
  • 50
  • 93
  • Refer to this link:http://stackoverflow.com/questions/24010035/how-to-add-image-and-text-in-uitextview-in-ios. – tounaobun Aug 31 '15 at 09:56
  • Is there perhaps another way? Because in that answer you have to define the position for the image. While my goal is to get the content with JSON from the same field if possible. – Sinan Samet Aug 31 '15 at 10:15
  • Or you could embed it into UIView. – tounaobun Aug 31 '15 at 10:17
  • That's fine too I also read that it would work with webview but how would I embed it? – Sinan Samet Aug 31 '15 at 10:20
  • Could you post your JSON content sample? – tounaobun Aug 31 '15 at 10:23
  • Well currently I didnt use images in my JSON content because I don't know how I should do it. But it would be nice if it would be possible to do it the way it is in HTML like: `"Test content"` – Sinan Samet Aug 31 '15 at 10:30
  • That's the possible way. – tounaobun Aug 31 '15 at 10:46
  • Would that be possible with webview? – Sinan Samet Aug 31 '15 at 11:36
  • If it's already in HTML, you still can use NSAttributedString, but you don't have to worry with NSTextAttachement, there is a method to convert HTML to NSAttributedString that will take care of that. Else, you have to use NSAttributedString (and you don't have to manage the position). – Larme Aug 09 '16 at 14:08

1 Answers1

0

You must create a json parser and create a NSTextAttachment for each image:

 let textAttachment = NSTextAttachment()
 textAttachment.image = yourImage
 let attrStringWithImage = NSAttributedString.init(attachment: textAttachment)
 attributedString.replaceCharactersInRange(NSMakeRange(6, 1), withAttributedString: attrStringWithImage)
 textView.attributedText = attributedString
Marco Santarossa
  • 4,058
  • 1
  • 29
  • 49