Is this possible to insert the image int开发者_如何学运维o 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.
No you can not. Rather you can take UIImageView
, set image and take transparent UITextView
and set your text view on image view.
No it cannot be done. You want to create a UIView and add a UIImageView and a UITextView as subviews of it.
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.
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);
}
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"]];
}
精彩评论