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. 任意List 和DatabTable的转换

     public static IEnumerable<T> ToEntityList<T>(this DataTable table) where T : class      ...

  2. hdu 2857 求点关于线段的对称点

    本来很简单的一个题,但是有个大坑: 因为模板中Tline用到了直线的一般方程ax+by+c=0,所以有种很坑的情况需要特判: 斜率不存在啊喂 老子坑了一下午2333 #include <math ...

  3. linux最常用的20条命令

    玩过Linux的人都会知道,Linux中的命令的确是非常多,但是玩过Linux的人也从来不会因为Linux的命令如此之多而烦恼,因为我们只需要掌握我们最常用的命令就可以了.当然你也可以在使用时去找一下 ...

  4. POJ2010 Moo University - Financial Aid(二分法)

    题目地址 分析:如果用二分法,关键是score和aid分开排序,score排序是为了充分利用中位数的性质,这样就可以确定m左右必须各选N/2个,到这之后有人是用dp求最优解,可以再次按照aid排序一次 ...

  5. Centos下 为Firefox安装Flash插件

    方法一: 直接到官网去下载RPM格式的安装包,下载好后直接 用 rpm -ivh XXXX.rpm 来安装即可. 方法二: 到官网下载tar.gz格式的,自己配置安装: #wget http://fp ...

  6. AngularJs ngList、ngRepeat、ngModelOptions

    ngList 在文本输入的分隔的字符串和字符串数组间做转换,可以是一个固定的字符串分隔符(默认逗号)或正则表达式. 格式:ng-list=”value” value:表达式  通过这个值分隔字符串. ...

  7. Beta版本冲刺第三天 12.9

    一.站立式会议照片: 二.项目燃尽图: Android端 后台 三.项目进展: 成 员 上次完成任务 今天完成任务 明天要做任务 问题困难 心得体会 胡泽善 完成用户简历的填写和查看 日期合理性的判断 ...

  8. Python ValueError: IO operation on closed file

    ValueError IO operation on closed file表示处理了已经被关闭的数据,在python 中 with语句的上下文会帮助处理,也就是说,当python的处理代码不对齐的时 ...

  9. BZOJ3226: [Sdoi2008]校门外的区间

    感觉很有趣的题呢. 每个点拆成两个,线段树维护. 不过这题难点其实在输入输出. #include<bits/stdc++.h> #define N (1<<17) #defin ...

  10. 利用Objective-C运行时hook函数的三种方法

    版权声明:转载请注明出处:http://blog.csdn.net/hursing 方法一,hook已有公开头文件的类: 首先写一个Utility函数: #import <objc/runtim ...