将图像添加到UITextView

use*_*721 12 iphone objective-c uitextview uiimageview ipad

在我的应用程序中,我UITextView在文本视图下方有一个按钮,可以UITextView在编辑时插入照片.

我的要求是用户用户能够在其中编辑文本,并在需要时插入图像.

与StackOverflow的应用程序类似UITextView:

在此输入图像描述

Mid*_* MP 18

您可以将图像视图添加为子视图UITextView.

使用image创建imageView:

UIImageView *imageView = [[UIImageView alloc] initWithImage:yourImage];
[imageView setFrame:yourFrame];
[yourTextView addSubview:imageView];
Run Code Online (Sandbox Code Playgroud)

编辑:

为避免重叠使用(Thanks @chris):

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]; 
Run Code Online (Sandbox Code Playgroud)

  • 但是,如何防止imageview覆盖textview中的文本?如何让文本在imageview中流动? (9认同)
  • 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]; (5认同)

Ole*_*hko 10

如果仅将其添加为子视图,则某些文本可以在图像"后面".因此,添加将"告诉"文本的代码,图像的区域是不可访问的:

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

textView.textContainer.exclusionPaths = @[exclusionPath];
Run Code Online (Sandbox Code Playgroud)