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

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 一起滚动,必须在滚动监听里动态改变 imageView y
-( 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、付费专栏及课程。

余额充值