Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Images to UITextView

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 that the user user is able to edit the text within, and insert images when needed.

Similar to StackOverflow's app's own UITextView:

enter image description here

like image 377
user1645721 Avatar asked Nov 07 '12 05:11

user1645721


2 Answers

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];
like image 132
Oleshko Avatar answered Oct 13 '22 12:10

Oleshko


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]; 
like image 29
Midhun MP Avatar answered Oct 13 '22 11:10

Midhun MP