iOS实现文字环绕图片textView布局

本文介绍了一种在iOS应用中实现图片与文本混排的方法,通过调整UIView坐标系,利用UITextView的exclusionPaths属性来实现图片周围的文本自动绕排效果,并通过监听scrollView的滚动事件确保图片与文本同步滚动。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CGRect textViewRect = CGRectInset(self.view.bounds, 10.0, 20.0);
    _myTextView = [[UITextView alloc] initWithFrame:textViewRect];
    _myTextView.text = text;
    _myTextView.editable = NO;
    _myTextView.delegate = self;
    [self.view insertSubview:_myTextView belowSubview:_imageView];
   
    //使用TextView_textView.textContainer.exclusionPath属性指定环绕路径
    _myTextView.textContainer.exclusionPaths = @[[self translatedBezierPath]];
//为了让图片跟随textView一起滚动,必须在滚动监听里动态改变imageViewy
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    _imageView.frame  = CGRectMake(_imageView.frame.origin.x,_imageViewY-scrollView.contentOffset.y,_imageView.frame.size.width , _imageView.frame.size.height);
   
}

- (UIBezierPath *)translatedBezierPath
{
    //坐标转换,原先imageView默认是以self.view作为参考系,现在要设置textView的环绕,所以将参考坐标系换成textView
    CGRect imageRect = [_myTextView convertRect:_imageView.frame fromView:self.view];
    //UIBezierPath设置为图片frame
    UIBezierPath *newPath = [UIBezierPath bezierPathWithRect:imageRect];
    return newPath;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值