且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将图像添加到 UITextView

更新时间:2023-12-06 17:15:40

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

You can add the image view as a subView of UITextView.

用图像创建一个图像视图:

Create an imageView with image:

UIImageView *imageView = [[UIImageView alloc] initWithImage:yourImage];
[imageView setFrame:yourFrame];
[yourTextView addSubview:imageView];

为了避免重叠使用(感谢@chris):

For avoiding the overlapping use (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];