如何在iOS中围绕图像扭曲文字?

时间:2015-10-19 08:05:14

标签: ios objective-c iphone xcode

我有一个自定义表格单元格。在该表格单元格中,我想显示包含文本的 UIImage ,该文本将从服务器中获取。我希望我的UI像这样。

enter image description here

我有这段代码

UIBezierPath * imgRect = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 100, 100)];
self.textView.textContainer.exclusionPaths = @[imgRect]

修改

上面的代码会在文本视图中为图像创建一个空白区域,但是如何在该区域中显示图像?

2 个答案:

答案 0 :(得分:0)

在您的解决方案中,您必须创建一个UIImageView,将其原点设置为与textview相同,然后根据图像的大小调整imgRect的大小,

Superview - // imageview和textview的超级视图

  • UIImageView {0,0,100,100} //让我们说100x100是图像的大小

  • UITextView {0,0,300,300},排除 - {0,0,100,100} // 300x300是textview的大小,100x100是图片的大小,你也可以有更大的排除大小以使文本和图像之间具有更漂亮的间距

还有第二个解决方案,创建NSAttributedString并在其前面附加NSTextAttachment,但是当你这样做时,你必须已经拥有了Image本身,所以如果你是异步提取Image,那么第一个解决方案会更好

UIImage *img = [UIImage imageNamed:imgName];
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = img;
NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] init];
[attrStr appendAttributedString:attrStringWithImage];

答案 1 :(得分:0)

我通过将Imagview作为子视图添加到Texview中来解决它

UIBezierPath * imgRect = [UIBezierPath bezierPathWithRect:CGRectMake(0, 10, 100, 100)];
cell.tv_post.textContainer.exclusionPaths = @[imgRect];
        UIImageView *tv_image =[[UIImageView alloc]initWithFrame:CGRectMake(0, 10, 100, 100)];
[tv_image setImageWithURL:[NSURL URLWithString:[IMAGE_BASE_URL stringByAppendingString:user_post.post_video_thumbnail]]placeholderImage:[UIImage imageNamed:@"post_placeholder.png"]];
[cell.tv_post addSubview:tv_image];