DJEmotionPageView.m

// 发送点击广播(和android类似,区别在于android的广播是只要有上下文对象context,就可以发送)
// iOS中的通知发送和接收都是通过NSNotificationCenter完成
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
userInfo[DJEmotionDidSelctedEmotionKey] = btn.emotion; [[NSNotificationCenter defaultCenter] postNotificationName:DJEmotionDidSelectedNotification object:nil userInfo:userInfo];

DJComposeViewControll.m

- (void)registerNotificationReceiver {

    // 注册监听表情按钮点击通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onReceiveEmotionBtnClick:) name:DJEmotionDidSelectedNotification object:nil]; } /** 接收到表情按钮点击通知 */
- (void)onReceiveEmotionBtnClick:(NSNotification *)notification { NSDictionary *intent = notification.userInfo;
DJEmotion *emotion = intent[DJEmotionDidSelctedEmotionKey];
[self.textView insertEmotion:emotion]; }

DJEmotionTextView.m

- (void)insertEmotion:(DJEmotion *)emotion {

    // 插入表情
if (emotion.code) { // Emoji表情
[self insertText:[NSString emojiWithStringCode:emotion.code]];
} else if (emotion.png) { // 表情图片
NSString *emotionName = emotion.png;
NSString *imagePath;
if ([emotionName hasPrefix:@"d_"] || [emotionName hasPrefix:@"f_"] ||
[emotionName hasPrefix:@"h_"] || [emotionName hasPrefix:@"l_"] || [emotionName hasPrefix:@"o_"] || [emotionName hasPrefix:@"w_"]) {
imagePath = [NSString stringWithFormat:@"EmotionIcons/default/%@",emotion.png]; // 默认表情路径
} else if ([emotionName hasPrefix:@"lxh_"]) {
imagePath = [NSString stringWithFormat:@"EmotionIcons/lxh/%@",emotion.png]; // 浪小花表情路径
} // 以textView的原本内容为基础构造一个attrStr
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText]; // 构造表情附件
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:imagePath];
CGFloat attachmentWH = self.font.lineHeight;
attachment.bounds = CGRectMake(, -, attachmentWH, attachmentWH); // 连接表情图片
NSAttributedString *attachStr = [NSAttributedString attributedStringWithAttachment:attachment]; // 将附件文本插入到光标所在的位置
NSUInteger cursorLocation = self.selectedRange.location;
[attrStr insertAttributedString:attachStr atIndex:cursorLocation]; // 设置当前attrStr的字体,因为attrStr的字体无法通过textview.font属性来设置
[attrStr addAttribute:NSFontAttributeName value:self.font range:NSMakeRange(, attrStr.length)]; // 更新当前textView内容
self.attributedText = attrStr; // 修正当前光标位置(将光标移动到插入表情末尾,默认光标会跳转到所有文本最后)
self.selectedRange = NSMakeRange(cursorLocation + , ); } }

最终效果:

新浪微博客户端(47)-在TextView中插入表情的更多相关文章

  1. 【Swift】图文混排,ios开发中在textfield或textView中插入图片

    在ios开发中,我们一般都是在textfield或者textView中输入文字.当我们需要插入图片的时候其实也是很简单的 我们需要利用的textfield,textView的属性化文本,将图片以附件的 ...

  2. 如何在markdown中插入表情

    Markdown是很好用的呀,个人灰常的喜欢,也是灰常漂亮的,但是如何在写文章的时候插入表情呢,下面给出一部分代码

  3. 新浪微博客户端(49)-删除输入的Emotion表情

    DJComposePageView.m - (void)deleteInputEmotion { // 发通知 [[NSNotificationCenter defaultCenter] postNo ...

  4. mysql数据库中插入表情4个字节的

    这个问题,原因是UTF-8编码有可能是两个.三个.四个字节.Emoji表情或者某些特殊字符是4个字节,而Mysql的utf8编码最多3个字节,所以数据插不进去. 我的解决方案是这样的 1.在mysql ...

  5. android 新浪微博客户端的表情功能的实现

    这是一篇好文章,我转来收藏,技术的最高境界是分享. 最近在搞android 新浪微博客户端,有一些心得分享弄android客户端表情功能可以用以下思路1.首页把新浪的表情下载到本地一文件夹种,表情图片 ...

  6. 新浪微博客户端(50)-解决输入Emotion表情逐渐变小的问题

    UITextView+Extension.h #import <UIKit/UIKit.h> @interface UITextView (Extension) /** 插入属性文本 */ ...

  7. textView中判断文本长度,自定义表情长度为1,emoj表情长度为1,输入限制

      static const int MAX_LIMIT_NUMS = 100; /**< 输入个数限制 */ // self.inputNumberTipsLabel 控制器的view上一个用 ...

  8. Android 编辑框插入表情图片

    首先,把整理好的表情图片以及布局用到的一些图片导入到项目的res/drawable目录中. 然后,编辑res/layout目录下布局.xml文件,这里我把oschina客户端的布局代码贴上来,供大家参 ...

  9. android开发新浪微博客户端 完整攻略 [新手必读]

    开始接触学习android已经有3个礼拜了,一直都是对着android的sdk文档写Tutorials从Hello World到Notepad Tutorial算是初步入门了吧,刚好最近对微博感兴趣就 ...

随机推荐

  1. HTML 事件处理

    1.HTML事件处理程序 2.DOM0级事件处理程序 3.DOM2级事件处理程序 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transi ...

  2. Java多线程与并发库高级应用-传统线程同步通信技术

    面试题: 子线程循环10次,接着主线程循环100次,接着又回到子线程循环10次,接着又 主线程循环100次,如此循环50次,请写出程序 /** * 子线程循环10次,接着主线程循环100次,接着又回到 ...

  3. java.io.IOException: Timed out waiting 20000ms for a quorum of nodes to respond

    16-11-14 21:23:41,540 FATAL org.apache.hadoop.hdfs.server.namenode.FSEditLog: Error: starting log se ...

  4. Uva1624 Knots

    极其鬼畜的题. 初见根本没有思路. 二见根本没有思路. …… 多年(并不)之后突然想到,也许可以用链表模拟. 先用链表把每一个节点串起来,并对有覆盖的地方进行标记. 模拟解锁操作,如果一个节点和它所覆 ...

  5. Node 一个简单的HttpServer+Mysql的后台

    接收来自客户端的Post参数,通过Mysql查询,并以Json返回需要的信息,直接代码 createServer(); function createServer(){ //使用express创建HT ...

  6. [U3D 添加大地、天空,用第一视角看看自己做的世界]

    1.添加大地.天空. 在下方Assets文件夹里右键,导入素材包. Terrain(地形)和Skybox(天空盒子) 2.选中左边的Terrain,在inspector里,选择中间的刷子(地形),还有 ...

  7. 谈谈JavaScript MVC模式

    第一个是:没有使用mvc模式的: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h ...

  8. Codeforces 711E ZS and The Birthday Paradox

    传送门 time limit per test 2 seconds memory limit per test 256 megabytes input standard input output st ...

  9. HDU 1213 How Many Tables(并查集)

    传送门 Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Igna ...

  10. linux快速安装lamp环境

    我折腾过不少的lamp安装方式,lnmp也折腾过.不过因为windows下面apache用的比较多,各种配置也比较熟悉,因此最终还是选择了lamp这个架构. 由于是自己装虚拟机玩,所以各种安全措施都没 ...