在我的应用我有一个UITextView
,只是文本视图下方的按钮,插入照片到UITextView
同时编辑。
我的要求是,用户的用户能够编辑中的文本,并在需要时插入图片。
类似StackOverflow上的应用程序自身UITextView
:
在我的应用我有一个UITextView
,只是文本视图下方的按钮,插入照片到UITextView
同时编辑。
我的要求是,用户的用户能够编辑中的文本,并在需要时插入图片。
类似StackOverflow上的应用程序自身UITextView
:
您可以添加图像视图的子视图UITextView
。
创建图像的ImageView的:
UIImageView *imageView = [[UIImageView alloc] initWithImage:yourImage];
[imageView setFrame:yourFrame];
[yourTextView addSubview:imageView];
编辑:
为了避免重叠使用(感谢@克里斯):
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];
如果添加它只是作为一个子视图,一些文字可以是“落后”的形象。 因此,添加代码会“告诉”的文字,图像的该区域是不可访问:
UIBezierPath *exclusionPath = [UIBezierPath bezierPathWithRect:CGRectMake(CGRectGetMinX(imageView.frame),
CGRectGetMinY(imageView.frame), CGRectGetWidth(imageView.frame),
CGRectGetHeight(imageView.frame))];
textView.textContainer.exclusionPaths = @[exclusionPath];
只需添加为TextView的像波纹管一子视图..
[yourTextView addSubview:yourImageView];
检查了这一点, IOS-5-富文本编辑系列 。 在iOS 5中,你可以插入图片,并使用HTML文本。 您可能需要使用UIWebview
和WebKit。
您还可以检查与EGOTextView里面有很多的富文本编辑功能。
创建的UITextView的子类,并重写此方法
- (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;
}
}