通过CoreText和富文本,能够实现图文混排和点击事件。
在实现点击事件时,能够对指定文本实现背景高亮。demo地址:https://github.com/lihui1314/LHTIViewT.git
我做了一些简单的封装代码调用起来也很简单。LHTIView
为展示类;LHCoreTextData
图文解析类,LHImageData
图片解析类,LHHighlight
文字点击事件设置类。
示例代码:
LHHighlight*hightlight = [[LHHighlight alloc]init];
hightlight.tapBackgroundColor = [UIColor lightGrayColor];//高亮背景
hightlight.userInfo = @{@"name":@"lh"};
hightlight.tapAction = ^(NSRange rang, id userInfo, NSString *str) {
[self lh_arlert:userInfo[@"name"]];
};//点击事件回调
[muteStr lh_setHighlight:hightlight andRange:NSMakeRange(3, 24)];
CGFloat width = [UIScreen mainScreen].bounds.size.width-30;
LHTIView*view = [[LHTIView alloc]initWithFrame:CGRectMake(15, 30,width, 0)];
LHCoreTextData*coreData = [[LHCoreTextData alloc]initWithWidth:width];
LHImageData*imageData = [[LHImageData alloc]initWithAttributes:atts viewWidth:width];
imageData.image = [UIImage imageNamed:@"331545211074_.pic.jpg"];
imageData.loction = 45;//图片插入位置
imageData.imageInfo = @{@"name":@"331545211074_.pic.jpg"};
coreData.imageDataArray = [NSMutableArray arrayWithObjects:imageData, nil];
coreData.muteAttStr = muteStr;
view.coreTextData = coreData;
view.delegate = self;
[self.view addSubview:view];
图片点击代理方法:
-(void)lh_didClickImage:(UIImage *)image info:(id)info{
[self lh_arlert:info[@"name"]];
}
效果图