Reputation: 2719
I have a problem, how to replace image in uitextview
as icon, when I tap this icon, image is showing
this example
thanks every one
Upvotes: 0
Views: 600
Reputation: 8988
The image is a NSTextAttachment stored in the NSAttributedString so to change it to an icon you need to create your own NSTextAttachment subclass and replace the default attachment with your own subclass. In the subclass you need to return the icon as an image. I do something similar, except I just resize the image to suite the devices display area, but I keep the original image so it can be viewed at full resolution. If this sounds like what your want to do then let me know and I'll dig out the code. Btw you can check out the free app to see how the uitextview works, it's called iWalletFree, or iProjectFree. There are Mac and iOS versions and the code is different for each.
Here is a link to the post where I just posted some details on image handling in UITextView and NSTextView.
Upvotes: 1
Reputation: 2270
Create a reference to your imageview and remove it from superview and create new image view with your icon image and add it to your textview. If you add the image view in the top of the textview it will hide your text in the textview.
Adding textview
in your .m file
> UITextView *textView; UIImageView *testImageView;
>
> -(void)addMyTextViewWithImage{
> self.textView = [[UITextView alloc]initWithFrame:CGRectMake(50.0f, 50.0f, 200.0f, 200.0f)];
> self.textView.inputView = [[UIView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 0.0f, 0.0f)];
> self.textView.backgroundColor = [UIColor blackColor];
> self.textView.textColor = [UIColor whiteColor];
> self.textView.font = [UIFont fontWithName:@"HelveticaNeue-UltraLight" size:30];
> self.textView.text = @"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmo to factor tum
> poen legum odioque civiuda.";
> self.testImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"yourimage.png"]];
> [self.testImageView setFrame:CGRectMake(10, 10, 150, 30)];
> [self.textView addSubview:self.testImageView];
> [self.view addSubview:self.textView]; }
>
> -(void)removeAndUpdateWithIconImage{
> //Remove my imageview from text view
> [self.testImageView removeFromSuperView];
>
> //Add the imageview with desire frame and icon image
> self.testImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon.png"]];
> [self.testImageView setFrame:CGRectMake(10, 10, 150, 30)];
> [self.textView addSubview:self.testImageView];
>
>
> }
Upvotes: 1
Reputation: 2006
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];
if you want to show image by tapping on it then take UIButton & assign image to it & add it as subview of UITextView after tap just change UIButton's image from icon to your image
Upvotes: 1