iOS NSTextAttachment - 图文混排
苹果在iOS7中推出了一个新的类NSTextAttachment,它是做图文混排的利器,本文就是用这个类,只用50行代码实现文字与表情混排,当然也可以实现段落中的图文混排。
首先说一下文字和表情的混排:
先来做点儿准备工作,搞一个存放表情信息的plist文件
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Image" ofType:@"plist"];NSArray *face = [NSArray arrayWithContentsOfFile:filePath];NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:text];NSString *regex_emoji =@"\\[[a-zA-Z0-9\\/\\u4e00-\\u9fa5]+\\]";//匹配表情NSError *error =nil;NSRegularExpression *re = [NSRegularExpression regularExpressionWithPattern:regex_emoji options:NSRegularExpressionCaseInsensitive error:&error];if(!re) {NSLog(@"%@", [error localizedDescription]);return attributeString;}NSArray *resultArray = [re matchesInString:text options:0 range:NSMakeRange(0, text.length)];NSMutableArray *imageArray = [NSMutableArray arrayWithCapacity:resultArray.count];//根据匹配范围来用图片进行相应的替换for(NSTextCheckingResult *match in resultArray) {//获取数组元素中得到rangeNSRange range = [match range];//获取原字符串中对应的值NSString *subStr = [text substringWithRange:range];for(int i =0; i < face.count; i ++) {if([face[i][@"cht"] isEqualToString:subStr]) {//face[i][@"png"]就是我们要加载的图片//新建文字附件来存放我们的图片,iOS7才新加的对象NSTextAttachment *textAttachment = [[NSTextAttachment alloc]init];//给附件添加图片textAttachment.image= [UIImage imageNamed:face[i][@"png"]];//调整一下图片的位置,如果你的图片偏上或者偏下,调整一下bounds的y值即可textAttachment.bounds=CGRectMake(0, -8, textAttachment.image.size.width, textAttachment.image.size.height);//把附件转换成可变字符串,用于替换掉源字符串中的表情文字NSAttributedString *imageStr = [NSAttributedString attributedStringWithAttachment:textAttachment];//把图片和图片对应的位置存入字典中NSMutableDictionary *imageDic = [NSMutableDictionary dictionaryWithCapacity:2];[imageDic setObject:imageStr forKey:@"image"];[imageDic setObject:[NSValue valueWithRange:range]forKey:@"range"];//把字典存入数组中[imageArray addObject:imageDic];}}}//4、从后往前替换,否则会引起位置问题for(int i = (int)imageArray.count-1; i >=0; i--) {NSRange range;[imageArray[i][@"range"] getValue:&range];//进行替换[attributeString replaceCharactersInRange:range withAttributedString:imageArray[i][@"image"]];}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
以上代码段是写了一个工具类以实现图文混排,也可以为NSString写一个分类来实现。下面说一下用法:
NSString *content =@"文字加上表情[得意][酷][呲牙]";NSMutableAttributedString *attrStr = [Utility emotionStrWithString:content];_contentLabel.attributedText= attrStr;
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
有了上面的方法,要实现图片和文字的排布就更容易了,只需要将某些图片给它设置一个固定的字符对应即可。
具体方法如下:
NSString *praiseStr =@”路人甲、路人乙”;
NSString *praiseInfo = [NSString stringWithFormat:@”<点赞> %@”,praiseStr];
NSDictionary *attributesForAll =@{NSFontAttributeName:[UIFont systemFontOfSize:14.0],NSForegroundColorAttributeName:[UIColorgrayColor]};
NSMutableAttributedString*attrStr = [Utility exchangeString:@”<点赞>”withText:praiseInfoimageName:@”dynamic_love_blue”];
有什么不足或者bug欢迎及时沟通!
iOS NSTextAttachment - 图文混排相关推荐
- iOS自己总结的超级详细分解富文本大全(AttributedString),图文混排很轻松
前言 最近项目中对图文混排有一定的需求,例如价格,文字链接,文字颜色变化等要求,翻了很多资料,咱们对这些属性做了如下的总结,希望能在方便自己查阅! NSFontAttributeName 设置字体大小 ...
- ios 表情符号 键盘_iOS 表情键盘+gif聊天图文混排,看我的就够了
更新: 1.解决首次加载键盘卡顿的问题: 2.修改聊天布局方式,现在无需计算,更加丝滑. 前言: 之前做过[OC版本]和[swift版本]图文混排和表情键盘,说实在的很low,特别是键盘,整体只是实现 ...
- iOS火焰动画效果、图文混排框架、StackView效果、偏好设置、底部手势等源码
iOS精选源码 高性能图文混排框架,构架顺滑的iOS应用. 使用OpenGLE覆盖阿尔法通道视频动画播放器视图. 可选最大日期截至当日日期的日期轮选器ChooseDatePicker 简单轻量的图片浏 ...
- iOS - 图文混排技术方案分享
前言 不少同学在工作中都能遇到图文混排的需求.但是实现图文混排的技术方案有好几种,相应的方案优劣也有差别.今天和大家一起分享一下图文混排的技术方案以及我的选择. Demo和解析工具已经开源 GitHu ...
- iOS-Swift3富文本(UILable文本图文混排)
转载注明出处:http://blog.csdn.net/qxuewei/article/details/53213636 介绍下iOS开发中常用的符文布图文混排 需求: 邱学伟是大帅哥(加个笑脸图片) ...
- (一一二)图文混排中特殊文字的点击与事件处理
在上一篇文章(一一一)图文混排基础 -利用正则分割和拼接属性字符串中提到了对attributedText的特殊处理,将其中的话题.URL都用红色进行了表示,如下图所示: 本节要实现的功能是这样的att ...
- UITextView实现图文混排效果
用UITextView实现图文混排效果的展示,首先要禁用UITextView的编辑功能,将属性editable设置为NO 1.首先创建一个NSTextAttachment对象,这个对象有一个image ...
- 使用CoreText实现图文混排
2019独角兽企业重金招聘Python工程师标准>>> OS没有现成的支持图文混排的控件,而要用多个基础控件组合拼成图文混排这样复杂的排版,是件很苦逼的事情.对此的解决方案有使用Co ...
- Swift3.0 功能二 (表情键盘与图文混排)
随着iOS越来越多表情键盘以及图文混排的需求,本文运用Swift3.0系统的实现其功能以及封装调用方法,写的不好,如有错误望各位提出宝贵意见,多谢 项目源码地址: 相关知识点都有标识 项目源码地址 废 ...
最新文章
- C# 计时函数精度测试
- linux如何设置浏览器,如何从 命令行 设置默认浏览器?
- boost::graph模块实现DFS算法的测试程序
- 控制反转IOC与依赖注入DI
- 我的2017年前端之路总结
- .NET 6新特性试用 | 可空引用类型
- 【HDU - 2012】素数判定(水题,数论,打表)
- 吴孟达肝癌逝世:肝被透支的全过程曝光!
- 22解析函数的级数表示(一)
- SaaS架构设计之如何转化成SaaS多租户模式
- 在Spring MVC中使用FileUpload功能
- 通信与信息系统专业排名全国前十的…
- linux opendir路径_Linux下目录文件的操作(opendir,readdir,closedir) 以及DIR,dirent,stat等结构体详解...
- 今日GitHub热榜第一:最全中华古诗词数据库,收录30多万诗词
- 弘辽科技:掌握淘宝直通车的工作原理很有必要!其核心是什么?
- MacQQ消息防撤回
- 【计算机基础】计算机发展历程
- 金融反欺诈的底层逻辑
- 3dmax制作alpha通道贴图
- Google网络硬盘(GDrive):千呼万唤不出来