Combining text and images in `UITextView`

前端 未结 3 1075
醉酒成梦
醉酒成梦 2020-12-09 00:15

Before I begin, let me acknowledge that similar questions exist, some with answers, others without. However, they do not address my specific problem. If you know of any, ple

相关标签:
3条回答
  • 2020-12-09 00:52
    __block  NSTextAttachment *imageAttachment = [NSTextAttachment new];
            imageAttachment.bounds = CGRectMake(0, -5, 20, 20);
            NSAttributedString *stringWithImage = [NSAttributedString attributedStringWithAttachment:imageAttachment];
            [deCodedString replaceCharactersInRange:NSMakeRange(deCodedString.length, 0) withAttributedString:stringWithImage];
            incomingMessage.messageAttributedString = deCodedString;
    
        SDWebImageDownloader *downloader = [SDWebImageDownloader sharedDownloader];
        imageAttachment.image = [UIImage imageNamed:@"profile_main_placeholder"];
    
        [downloader downloadImageWithURL:aURL
                                 options:0
                                progress:^(NSInteger receivedSize, NSInteger expectedSize) {
                                    // progression tracking code
                                }
                               completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
                                   if (image && finished) {
                                       [image drawInRect:CGRectMake(0, 0, 20, 20)];
                                       imageAttachment.image = image;
    
                                       dispatch_async(dispatch_get_main_queue(), ^(void)
                                                      {
    
                                                          [self.tbl_Conversation reloadRowsAtIndexPaths:[self.tbl_Conversation indexPathsForVisibleRows]
                                                                                       withRowAnimation:UITableViewRowAnimationNone];
                                                          [self.tbl_Conversation reloadData];
                                                      });
    
    
    
                                       //                                                              NSAttributedString *stringWithImage = [NSAttributedString attributedStringWithAttachment:imageAttachment];
                                       //                                                              [deCodedString replaceCharactersInRange:NSMakeRange(deCodedString.length, 0) withAttributedString:stringWithImage];
                                       //                                                              incomingMessage.messageAttributedString = deCodedString;
                                   }
                               }];
    
    0 讨论(0)
  • 2020-12-09 00:52

    Actually, I solved this problem using the NSAttributedString class. It allows programmers to combine images in the text buffer and be treated just like a single character in the buffer which means hitting the space bar with the cursor before that image will move the image and all other text one space to the right. Likewise hitting the delete key before the image deletes it from the buffer. Hope it helps someone

    0 讨论(0)
  • 2020-12-09 01:02

    If I understand your goal, and how you currently have it implemented. You should check if you need to return first.

    if ((cursorPosition.x + 30) >= message.frame.width) {
        message.text = message.text.stringByAppendingString("\n");
    }
    

    Then you should get the now current cursor position, and add your UIImageView there.

        let size = CGSize(width: 30, height: 30);
        let img = UIImage(named: change_arr[indexPath.row]);
        let addImg = UIImageView(image: UIImage(named: change_arr[indexPath.row]));
        addImg.frame = CGRect(origin: newCursorPosition, size: size);
    message.addSubview(addImg);
    

    then if you need to add spaces, add them now.

    As stated in the previous comments using NSTextAttachment will simplify all of this, and prevent you from having to create UIViews, etc...

    it would look something like this, and you don't need to check for the cursor position, because it will handle that just like it does with text.

    //create your UIImage
    let image = UIImage(named: change_arr[indexPath.row]);
    //create and NSTextAttachment and add your image to it.
    let attachment = NSTextAttachment()
    attachment.image = image
    //put your NSTextAttachment into and attributedString
    let attString = NSAttributedString(attachment: attachment)
    //add this attributed string to the current position.
    textView.textStorage.insertAttributedString(attString, atIndex: textView.selectedRange.location)
    

    Now the image is inline, and treated like text. If the user backspaces over it, it deletes just like text.

    0 讨论(0)
提交回复
热议问题