0

I'm trying to add image to the text view text

enter image description here

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{

    UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
    [self updateTextViewWithImage:image];

    [picker dismissViewControllerAnimated:YES completion:nil];
}

-(void)updateTextViewWithImage:(UIImage *)image{

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:_textView.text];
    NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
    textAttachment.image = image;

    NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];
    [attributedString replaceCharactersInRange:NSMakeRange(2, 1) withAttributedString:attrStringWithImage];
    _textView.attributedText = attributedString;
}

In the xcode debugger, I do see that text attachemnt is added properly.

Before the method call:

   Hello{
    NSFont = "<UICTFont: 0x1004333f0> font-family: \".SFUIText\"; font-weight: normal; font-style: normal; font-size: 14.00pt";
    NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n    28L,\n    56L,\n    84L,\n    112L,\n    140L,\n    168L,\n    196L,\n    224L,\n    252L,\n    280L,\n    308L,\n    336L\n), DefaultTabInterval 0, Blocks (\n), Lists (\n), BaseWritingDirection -1, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0";
}

After the method call:

He{
    NSFont = "<UICTFont: 0x1004019c0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
    NSOriginalFont = "<UICTFont: 0x1004019c0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
}{
    NSAttachment = "<NSTextAttachment: 0x1702a2880>";
    NSFont = "<UICTFont: 0x1004019c0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
    NSOriginalFont = "<UICTFont: 0x1004019c0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
}lo{
    NSFont = "<UICTFont: 0x1004019c0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
    NSOriginalFont = "<UICTFont: 0x1004019c0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
}

But even after the method is called, the text view is not showing the image in it.Also textView is not nil.I am able to add the image as a subview to text view, but I would like to use the attributed string.

Edit: I also tried resizing the image to 100x100 and also set the attributed text in the storyboard. But still no image is added to text.

Strange thing is that, image is added to the attributed text, if the textView is created and added as a subview.

-(void)updateTextViewWithImage:(UIImage *)image{

    UIImage *img=[self imageWithImage:image scaledToSize:CGSizeMake(100, 100)];

    UITextView *textView = [[UITextView alloc] initWithFrame:_textView.frame];
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"before after"];
    NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
    textAttachment.image = img;

    NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];
    [attributedString replaceCharactersInRange:NSMakeRange(6, 1) withAttributedString:attrStringWithImage];
    textView.attributedText = attributedString;
    _textView.attributedText=attributedString;
    [_textView addSubview:textView];

}

THe image is not added, if the textView is created in IB.

Edit:

I checked the text view address, it is same at viewDidAppear and after I get the image.

<UITextView: 0x7d085a00   - at viewWillAppear

<UITextView: 0x7d085a00; - at updateTextViewWithImage:

In this edit, I texted with the _textView

-(void)updateTextViewWithImage:(UIImage *)image{

        UIImage *img=[self imageWithImage:image scaledToSize:CGSizeMake(100, 100)];

        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"before after"];
        NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
        textAttachment.image = img;

        NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];
        [attributedString replaceCharactersInRange:NSMakeRange(6, 1) withAttributedString:attrStringWithImage];
        _textView.attributedText=attributedString;

    }

After I call the updateTextViewWithImage , I checked the viewDebugger:

enter image description here

and the textView is not updated with the attributed string.

Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109
  • Could you check the address of `_textView` is it the same? Also, with XCode Debug Hierarchy Tool (https://developer.apple.com/library/ios/documentation/DeveloperTools/Conceptual/debugging_with_xcode/chapters/special_debugging_workflows.html#//apple_ref/doc/uid/TP40015022-CH9-SW2), could you check that it's okay ? I'm wondering if you are not doing an error since it's work when you add a new `UITextView` above that you might have already 2 textView one above the other and you are updating the one under it which is hidden by the one above. – Larme Aug 08 '16 at 15:22
  • @Larme, I updated the question. – Teja Nandamuri Aug 08 '16 at 15:38
  • @Larme, thanks for the tip, I found that textView text is overridden in the viewWillAppear method. – Teja Nandamuri Aug 08 '16 at 15:45

1 Answers1

0

I found that the mistake lies in my viewWillappearMethod.

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [_textView becomeFirstResponder];

    //get the notes from datatabse
    if ([_notesStatus isEqualToString:@"UpdateNotes"]) {

        _textView.text=[_myManager getStringForRowWithId:_notesID];
        NSLog(@"text view addres %@",_textView);

    }
}

After the camera picker is dismissed, the viewWillAppear is called, and _textView.text is overriding the attributed string.

Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109