Iphone 向UITextView添加图像
在我的应用程序中,我有一个Iphone 向UITextView添加图像,iphone,objective-c,ipad,uiimageview,uitextview,Iphone,Objective C,Ipad,Uiimageview,Uitextview,在我的应用程序中,我有一个UITextView,在文本视图下方有一个按钮,可以在编辑时将照片插入UITextView 我的要求是,用户能够编辑其中的文本,并在需要时插入图像 类似于StackOverflow的应用程序自己的UITextView: 您可以将图像视图添加为UITextView的子视图 使用图像创建imageView: UIImageView *imageView = [[UIImageView alloc] initWithImage:yourImage]; [imageView s
UITextView
,在文本视图下方有一个按钮,可以在编辑时将照片插入UITextView
我的要求是,用户能够编辑其中的文本,并在需要时插入图像
类似于StackOverflow的应用程序自己的UITextView
:
您可以将图像视图添加为
UITextView
的子视图
使用图像创建imageView:
UIImageView *imageView = [[UIImageView alloc] initWithImage:yourImage];
[imageView setFrame:yourFrame];
[yourTextView addSubview:imageView];
编辑:
为避免重复使用(感谢@chris):
看看这个。在iOS 5中,您可以插入图像并使用HTML文本。您可能必须使用UIWebview
和webkit
您还可以检查其中是否有许多富文本编辑功能。只需添加为TextView的一个子视图,如bellow
[yourTextView addSubview:yourImageView];
如果仅将其作为子视图添加,则某些文本可能位于图像的“后面”。 因此,添加“告诉”文本的代码,该图像区域不可访问:
UIBezierPath *exclusionPath = [UIBezierPath bezierPathWithRect:CGRectMake(CGRectGetMinX(imageView.frame),
CGRectGetMinY(imageView.frame), CGRectGetWidth(imageView.frame),
CGRectGetHeight(imageView.frame))];
textView.textContainer.exclusionPaths = @[exclusionPath];
创建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;
}
}
@user1645721,如果您希望用户在图像后开始输入文本,您可能必须在我的回答中使用上述建议。但是如何防止imageview覆盖文本视图中的文本?如何使文本在imageview中流动;[附件视图设置框架:aRect];UIBezierPath*ExclutionPath=[UIBezierPath bezierPathWithRect:CGRectMake(CGRectGetMinX(attachmentView.frame)、CGRectGetMinY(attachmentView.frame)、CGRectGetWidth(internalTextView.frame),CGRectGetHeight(attachmentView.frame));internalTextView.textContainer.ExclutionPath=@[ExclutionPath];[internalTextView addSubview:attachmentView];
- (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;
}
}