5

What I am doing is:

  1. NSATTRIBUTE STRING = NSSTRING + UIIMAGE's;
  2. NSDATA = NSATTRIBUTED STRING;
  3. ALSO I am able to convert nsdata to nsattributed string
  4. NSATTRIBUTED STRING = NSDATA:
  5. And then extracting nesting from NSAttributed string
  6. NSSTRING = [NSATTRIBUTED STRING string];

Query:

How can i get IMAGES from NSATTRIBUTED STRING;

  • UIIMAGE = from NSATTRIBUTED STRING;
  • ARRAYOFIMAGE = from NSATTRIBUTED STRING;
Yogesh Lolusare
  • 2,162
  • 1
  • 24
  • 35

3 Answers3

17

You have to enumerate the NSAttributedString looking for NSTextAttachments.

NSMutableArray *imagesArray = [[NSMutableArray alloc] init];
[attributedString enumerateAttribute:NSAttachmentAttributeName
                             inRange:NSMakeRange(0, [attributedString length])
                             options:0
                          usingBlock:^(id value, NSRange range, BOOL *stop)
{
    if ([value isKindOfClass:[NSTextAttachment class]])
    {
    NSTextAttachment *attachment = (NSTextAttachment *)value;
    UIImage *image = nil;
    if ([attachment image])
        image = [attachment image];
    else
        image = [attachment imageForBounds:[attachment bounds]
                             textContainer:nil
                            characterIndex:range.location];

    if (image)
        [imagesArray addObject:image];
    }
}];

As you can see, there is the test if ([attachment image]). That's because it seems that if you created the NSTextAttachment to put with NSAttachmentAttributeName it will exist and your image will be there. But if you use for example an image from the web and convert it as a NSTextAttachment from a HTML code, then [attachment image] will be nil and you won't be able to get the image.

You can see using breakpoints with this snippet (with setting real image URL and an real image name from bundle. NSString *htmlString = @"http://anImageURL\">Blahttp://anOtherImageURL\"> Test retest";

NSError *error;
NSAttributedString *attributedStringFromHTML = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding]
                                                                                options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,
                                                                                          NSCharacterEncodingDocumentAttribute:@(NSUTF8StringEncoding)}
                                                                     documentAttributes:nil
                                                                                  error:&error];

NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
[textAttachment setImage:[UIImage imageNamed:@"anImageNameFromYourBundle"]];

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:attributedStringFromHTML];
[attributedString appendAttributedString:[NSAttributedString attributedStringWithAttachment:textAttachment]];
Larme
  • 24,190
  • 6
  • 51
  • 81
  • Thanks. I will implement the given solution & will updated you. – Yogesh Lolusare Mar 19 '15 at 19:36
  • Thanks so much for this. Even if I set my own text attachments, when I try to extract them again, `image` is nil, but `imageForBounds` works! Not much info out there, this looks like a bug to me. – Tim May 22 '16 at 13:43
  • @larme how to set in label – Mss iOS Jun 09 '17 at 09:28
  • @MssiOS Could you be more explicit? What's your issue exactly with the solution? Is your issue similar to the author's question? – Larme Jun 09 '17 at 09:29
  • Sir Here is the mY Question https://stackoverflow.com/questions/44364002/how-to-show-custom-emojis-url-link-in-labels-ios – Mss iOS Jun 09 '17 at 09:30
4

In Swift 3: (with macOS equivalent here)

func textViewDidChange(_ textView: UITextView) {

    // other code...

    let range = NSRange(location: 0, length: textView.attributedText.length)
    if (textView.textStorage.containsAttachments(in: range)) {
        let attrString = textView.attributedText
        var location = 0
        while location < range.length {
            var r = NSRange()
            let attrDictionary = attrString?.attributes(at: location, effectiveRange: &r)
            if attrDictionary != nil {
                // Swift.print(attrDictionary!)
                let attachment = attrDictionary![NSAttachmentAttributeName] as? NSTextAttachment
                if attachment != nil {
                    if attachment!.image != nil {
                        // your code to use attachment!.image as appropriate
                    }
                }
                location += r.length
            }
        }
    }
}
Community
  • 1
  • 1
JKaz
  • 765
  • 6
  • 18
3

I converted the code Larme to swift 3

      var imagesArray = [Any]()

      textView.attributedText.enumerateAttribute(NSAttachmentAttributeName, in: NSRange(location: 0, length: textView.attributedText.length), options: [], using: {(value,range,stop) -> Void in
            if (value is NSTextAttachment) {
                let attachment: NSTextAttachment? = (value as? NSTextAttachment)
                var image: UIImage? = nil

                if ((attachment?.image) != nil) {
                    image = attachment?.image 
                } else {
                    image = attachment?.image(forBounds: (attachment?.bounds)!, textContainer: nil, characterIndex: range.location)
                }

                if image != nil {
                  imagesArray.append(image)
                }
            }
        })
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Vasya2014
  • 203
  • 1
  • 5
  • 25