苹果在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 - 图文混排相关推荐

  1. iOS自己总结的超级详细分解富文本大全(AttributedString),图文混排很轻松

    前言 最近项目中对图文混排有一定的需求,例如价格,文字链接,文字颜色变化等要求,翻了很多资料,咱们对这些属性做了如下的总结,希望能在方便自己查阅! NSFontAttributeName 设置字体大小 ...

  2. ios 表情符号 键盘_iOS 表情键盘+gif聊天图文混排,看我的就够了

    更新: 1.解决首次加载键盘卡顿的问题: 2.修改聊天布局方式,现在无需计算,更加丝滑. 前言: 之前做过[OC版本]和[swift版本]图文混排和表情键盘,说实在的很low,特别是键盘,整体只是实现 ...

  3. iOS火焰动画效果、图文混排框架、StackView效果、偏好设置、底部手势等源码

    iOS精选源码 高性能图文混排框架,构架顺滑的iOS应用. 使用OpenGLE覆盖阿尔法通道视频动画播放器视图. 可选最大日期截至当日日期的日期轮选器ChooseDatePicker 简单轻量的图片浏 ...

  4. iOS - 图文混排技术方案分享

    前言 不少同学在工作中都能遇到图文混排的需求.但是实现图文混排的技术方案有好几种,相应的方案优劣也有差别.今天和大家一起分享一下图文混排的技术方案以及我的选择. Demo和解析工具已经开源 GitHu ...

  5. iOS-Swift3富文本(UILable文本图文混排)

    转载注明出处:http://blog.csdn.net/qxuewei/article/details/53213636 介绍下iOS开发中常用的符文布图文混排 需求: 邱学伟是大帅哥(加个笑脸图片) ...

  6. (一一二)图文混排中特殊文字的点击与事件处理

    在上一篇文章(一一一)图文混排基础 -利用正则分割和拼接属性字符串中提到了对attributedText的特殊处理,将其中的话题.URL都用红色进行了表示,如下图所示: 本节要实现的功能是这样的att ...

  7. UITextView实现图文混排效果

    用UITextView实现图文混排效果的展示,首先要禁用UITextView的编辑功能,将属性editable设置为NO 1.首先创建一个NSTextAttachment对象,这个对象有一个image ...

  8. 使用CoreText实现图文混排

    2019独角兽企业重金招聘Python工程师标准>>> OS没有现成的支持图文混排的控件,而要用多个基础控件组合拼成图文混排这样复杂的排版,是件很苦逼的事情.对此的解决方案有使用Co ...

  9. Swift3.0 功能二 (表情键盘与图文混排)

    随着iOS越来越多表情键盘以及图文混排的需求,本文运用Swift3.0系统的实现其功能以及封装调用方法,写的不好,如有错误望各位提出宝贵意见,多谢 项目源码地址: 相关知识点都有标识 项目源码地址 废 ...

最新文章

  1. C# 计时函数精度测试
  2. linux如何设置浏览器,如何从 命令行 设置默认浏览器?
  3. boost::graph模块实现DFS算法的测试程序
  4. 控制反转IOC与依赖注入DI
  5. 我的2017年前端之路总结
  6. .NET 6新特性试用 | 可空引用类型
  7. 【HDU - 2012】素数判定(水题,数论,打表)
  8. 吴孟达肝癌逝世:肝被透支的全过程曝光!
  9. 22解析函数的级数表示(一)
  10. SaaS架构设计之如何转化成SaaS多租户模式
  11. 在Spring MVC中使用FileUpload功能
  12. 通信与信息系统专业排名全国前十的…
  13. linux opendir路径_Linux下目录文件的操作(opendir,readdir,closedir) 以及DIR,dirent,stat等结构体详解...
  14. 今日GitHub热榜第一:最全中华古诗词数据库,收录30多万诗词
  15. 弘辽科技:掌握淘宝直通车的工作原理很有必要!其核心是什么?
  16. MacQQ消息防撤回
  17. 【计算机基础】计算机发展历程
  18. 金融反欺诈的底层逻辑
  19. 3dmax制作alpha通道贴图
  20. Google网络硬盘(GDrive):千呼万唤不出来

热门文章

  1. 分布式锁是啥?zk还是redis?
  2. [二叉树]序列化二叉树 (剑指offer61)
  3. Kotlin 第三讲——集合篇1
  4. 借呗利息为什么比银行信用贷款高很多?
  5. 过年遇到前任借钱, 如何傲娇的拒绝?
  6. 背账100万,不付利息不用还钱,银行套路好深
  7. 贷款审批到底会考核哪些内容?
  8. layui 金额数据千分位_IG神秘打野韩服数据,盲僧数据或暗示英雄池问题
  9. matlab 邻近度 离群点_MATLAB自制迷宫游戏,快来试试吧!
  10. java复制的函数会报错,2 面试题之面向对象