iOS图文环绕

项目中用到图文环绕效果,如图


效果.png

实现方式:


image.png
@interface MainPageView ()
@property (nonatomic, strong) UITextView * textView;
@end
- (void)viewDidLoad {
    [super viewDidLoad];
    [self getVenueContent];
}

-(void)getVenueContent{
    self.edgesForExtendedLayout=UIRectEdgeNone;
    self.textView=[[UITextView alloc]initWithFrame:_contentView.frame];
    self.textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 0, [UIScreen mainScreen].bounds.size.width-10 , [UIScreen mainScreen].bounds.size.height - 40 - 44 )];
//    self.textView.backgroundColor = [UIColor cyanColor];
    self.textView.text = @"南京博物院位于南京市玄武区中山东路321号,简称南院或南博,是中国三大博物馆之一,其前身是1933年蔡元培等倡建的国立中央博物院,是中国创建最早的博物馆、中国第一座由国家投资兴建的大型综合类博物馆。南京博物院是大型综合性的国家级博物馆、国家综合性历史艺术博物馆,现为国家一级博物馆、首批中央地方共建国家级博物馆、国家AAAA级旅游景区和全国重点文物保护单位。";
    self.textView.font = [UIFont systemFontOfSize:15];
    self.textView.editable = NO;

    [self.view addSubview:self.textView];
    
    //图文混排,设置图片的位置
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 100, 100)];
    imageView.image = [UIImage imageNamed:@"011"];
    //imageView.image = [self imageFromURLString:@"192.168.1.136:8080/images/venue.jpg"];
    imageView.backgroundColor = [UIColor redColor];
    [self.view addSubview:imageView];
    CGRect rect = CGRectMake(10, 0, 100, 100);
    
    //设置环绕的路径
    UIBezierPath * path = [UIBezierPath bezierPathWithRect:rect];
    self.textView.textContainer.exclusionPaths = @[path];
}
/*
- (UIImage *) imageFromURLString: (NSString *) urlstring
{
    return [UIImage imageWithData:[NSData  dataWithContentsOfURL:[NSURL URLWithString:urlstring]]];
}
*/

参考链接:https://blog.csdn.net/liujinlongxa/article/details/44840753

你可能感兴趣的:(iOS图文环绕)