2012-11-07 189 views
11

在我的应用程序中,我有一个UITextView和一个紧靠文本视图的按钮,以便在编辑时将一张照片插入到UITextView中。将图像添加到UITextView

我的要求是用户用户能够编辑文本内容,并在需要时插入图像。

到StackOverflow上的应用程序自身UITextView类似:

enter image description here

回答

17

您可以添加图像视图为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]; 
+9

但你如何防止从ImageView的覆盖textview中的文本?你如何让文字在imageview中流动? –

+5

CGRect aRect = CGRectMake(156,8,16,16); [attachmentView setFrame:aRect]; UIBezierPath * exclusionPath = [UIBezierPath bezierPathWithRect:CGRectMake(CGRectGetMinX(attachmentView.frame) CGRectGetMinY(attachmentView.frame),CGRectGetWidth(internalTextView.frame) CGRectGetHeight(attachmentView.frame))]; internalTextView.textContainer.exclusionPaths = @ [exclusionPath]; [internalTextView addSubview:attachmentView]; –

1

检查了这一点,ios-5-rich-text-editing-series。在iOS 5中,您可以插入图像并使用HTML文本。您可能必须使用UIWebview和webkit。

您还可以使用EGOTextView进行检查,它具有丰富的文本编辑功能。

+0

@ user1645721,如果您希望用户在图片后面开始输入文字,您可能必须在我的回答中使用上述建议。 – iDev

1

只是添加为TextView的像波纹管一子视图..

[yourTextView addSubview:yourImageView]; 
9

如果添加它只是作为一个子视图,一些文字可以是“落后”的形象。 所以添加代码会“告诉”的文字,图像的该区域是不可访问:

UIBezierPath *exclusionPath = [UIBezierPath bezierPathWithRect:CGRectMake(CGRectGetMinX(imageView.frame),  
CGRectGetMinY(imageView.frame), CGRectGetWidth(imageView.frame), 
CGRectGetHeight(imageView.frame))]; 

textView.textContainer.exclusionPaths = @[exclusionPath]; 
0

创建的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; 

    } 
}