Reputation: 409
Is this possible to insert the image into the UITextView from the Gallery. If this is possible then how this can be done? Is there any default method of the UITextView? Please suggest me.
Upvotes: 2
Views: 2890
Reputation: 121
It might be already solved but if someone still looking for it then.
- (void)addImage:(UIImage *)image inTextView:(UITextView *)textView
{
if (!image ||
!textView)
{
return;
}
CGFloat padding = 10; // 10px for the padding for displaying image nicely
CGFloat scaleFactor = image.size.width / (CGRectGetWidth(self.textView.frame) - padding);
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = [UIImage imageWithCGImage:image.CGImage scale:scaleFactor orientation:UIImageOrientationUp];
NSAttributedString *attributedStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];
[self.textView.textStorage appendAttributedString:attributedStringWithImage];
// Uncomment following line if you want to have a new line after image
// [self.textView.textStorage appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];
}
Upvotes: 0
Reputation: 2281
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
[textView setContentInset:UIEdgeInsetsMake(0, 0, 60, 0)];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(130, textView.contentSize.height, 60, 60)];
view.backgroundColor = [UIColor greenColor]; // for testing
[textView addSubView:view];
UIImageView *imgView = [[UiImageView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
[imgView setImage:[UIImage imageNamed:@"anyImage.png"]];
[view addSubView:imgView];
// catching textViewDidChange delegate method
- (void)textViewDidChange:(UITextView *)textView {
view.frame = CGRectMake(130, textView.contentSize.height, 60, 60);
}
Upvotes: 0
Reputation: 4879
As others said, you can't. If you want that because you want your text wrap around your image, I suggest that you use UIWebView
and html instead.
Upvotes: 1
Reputation: 19418
No you can not. Rather you can take UIImageView
, set image and take transparent UITextView
and set your text view on image view.
Upvotes: 2
Reputation: 16714
No it cannot be done. You want to create a UIView and add a UIImageView and a UITextView as subviews of it.
Upvotes: 1