Adding Images to UITextView

后端 未结 5 2121
灰色年华
灰色年华 2020-12-01 04:38

In my app I have a UITextView and a button just below the text view to insert a photo into the UITextView while editing.

My requirement is

相关标签:
5条回答
  • 2020-12-01 05:15

    If you add it only as a subview, some text can be "behind" the image. So add the code which will "tell" the text, that area of the image is inaccessible:

    UIBezierPath *exclusionPath = [UIBezierPath    bezierPathWithRect:CGRectMake(CGRectGetMinX(imageView.frame),    
    CGRectGetMinY(imageView.frame), CGRectGetWidth(imageView.frame), 
    CGRectGetHeight(imageView.frame))];
    
    textView.textContainer.exclusionPaths = @[exclusionPath];
    
    0 讨论(0)
  • 2020-12-01 05:18

    Check this out, ios-5-rich-text-editing-series . In iOS 5 you can insert images and use HTML texts. You might have to use UIWebview and webkit.

    You can also check with EGOTextView which has a lot of rich text editing features.

    0 讨论(0)
  • 2020-12-01 05:22

    Create subclass of UITextView and override this method

    - (void)paste:(id)sender 
    {
        NSData *data = [[UIPasteboard generalPasteboard] dataForPasteboardType:@"public.png"];
    
        if (data) 
        {
    
            NSMutableAttributedString *attributedString = [[self attributedText] mutableCopy];
    
            NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
    
            textAttachment.image = [UIImage imageWithData:data scale:5];
    
            NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];
    
            [attributedString replaceCharactersInRange:self.selectedRange withAttributedString:attrStringWithImage];
    
            self.attributedText = attributedString;
    
        }
        else
        {
    
            UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
    
            NSAttributedString *text = [[NSAttributedString alloc] initWithString:pasteBoard.string];
    
            NSMutableAttributedString *attributedString = [self.attributedText mutableCopy];
    
            [attributedString replaceCharactersInRange:self.selectedRange withAttributedString:text];
    
            self.attributedText = attributedString;
    
        }
    }
    
    0 讨论(0)
  • 2020-12-01 05:31

    You can add the image view as a subView of UITextView.

    Create an imageView with image:

    UIImageView *imageView = [[UIImageView alloc] initWithImage:yourImage];
    [imageView setFrame:yourFrame];
    [yourTextView addSubview:imageView];
    

    Edit:

    For avoiding the overlapping use (Thanks @chris):

    CGRect aRect = CGRectMake(156, 8, 16, 16);
    [imageView setFrame:aRect];
    UIBezierPath *exclusionPath = [UIBezierPath bezierPathWithRect:CGRectMake(CGRectGetMinX(imageView.frame), CGRectGetMinY(imageView.frame), CGRectGetWidth(yourTextView.frame), CGRectGetHeight(imageView.frame))];
    yourTextView.textContainer.exclusionPaths = @[exclusionPath];
    [yourTextView addSubview:imageView]; 
    
    0 讨论(0)
  • 2020-12-01 05:31

    just add as a subview of TextView like bellow..

        [yourTextView addSubview:yourImageView];
    
    0 讨论(0)
提交回复
热议问题