新浪微博客户端(47)-在TextView中插入表情
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中插入表情的更多相关文章
- 【Swift】图文混排,ios开发中在textfield或textView中插入图片
在ios开发中,我们一般都是在textfield或者textView中输入文字.当我们需要插入图片的时候其实也是很简单的 我们需要利用的textfield,textView的属性化文本,将图片以附件的 ...
- 如何在markdown中插入表情
Markdown是很好用的呀,个人灰常的喜欢,也是灰常漂亮的,但是如何在写文章的时候插入表情呢,下面给出一部分代码
- 新浪微博客户端(49)-删除输入的Emotion表情
DJComposePageView.m - (void)deleteInputEmotion { // 发通知 [[NSNotificationCenter defaultCenter] postNo ...
- mysql数据库中插入表情4个字节的
这个问题,原因是UTF-8编码有可能是两个.三个.四个字节.Emoji表情或者某些特殊字符是4个字节,而Mysql的utf8编码最多3个字节,所以数据插不进去. 我的解决方案是这样的 1.在mysql ...
- android 新浪微博客户端的表情功能的实现
这是一篇好文章,我转来收藏,技术的最高境界是分享. 最近在搞android 新浪微博客户端,有一些心得分享弄android客户端表情功能可以用以下思路1.首页把新浪的表情下载到本地一文件夹种,表情图片 ...
- 新浪微博客户端(50)-解决输入Emotion表情逐渐变小的问题
UITextView+Extension.h #import <UIKit/UIKit.h> @interface UITextView (Extension) /** 插入属性文本 */ ...
- textView中判断文本长度,自定义表情长度为1,emoj表情长度为1,输入限制
static const int MAX_LIMIT_NUMS = 100; /**< 输入个数限制 */ // self.inputNumberTipsLabel 控制器的view上一个用 ...
- Android 编辑框插入表情图片
首先,把整理好的表情图片以及布局用到的一些图片导入到项目的res/drawable目录中. 然后,编辑res/layout目录下布局.xml文件,这里我把oschina客户端的布局代码贴上来,供大家参 ...
- android开发新浪微博客户端 完整攻略 [新手必读]
开始接触学习android已经有3个礼拜了,一直都是对着android的sdk文档写Tutorials从Hello World到Notepad Tutorial算是初步入门了吧,刚好最近对微博感兴趣就 ...
随机推荐
- matplotlib 和 pandas 两个包的安装
matplotlib是强大的python 绘图包.pandas 是强大的python分析工具包.numpy是强大的python统计包. 都超级好用,而且最近开始动手实践机器学习算法了.特此备注一下安装 ...
- SQL怎么输出前n个记录? n是中间计算得到的,不支持变量传递
需求: 表 people_crowed_test 按view_num排序后,输出该表的记录前30%的aid, buyer_id; 需求场景下的诸多限制: 1) 不支持变量赋值,也就是无法把中间结果保存 ...
- 【BZOJ-2768】冠军调查 最小割
2768: [JLOI2010]冠军调查 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 971 Solved: 661[Submit][Status ...
- 【bzoj2654】 tree
http://www.lydsy.com/JudgeOnline/problem.php?id=2654 (题目链接) 题意 给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有nee ...
- NuGet在创建pack时提示”The replacement token 'author' has no value“问题解决
在创建pack时出现了“The replacement token 'author' has no value”的错误提示. 解决方法: 1.可能程序没生成过,在解决方案上重新生成解决方案,注意Deb ...
- array数组加过滤
var array = new Array(); array.push(0); array.push(1); array.push(2); var arr = array.filter(functio ...
- C#产生随机颜色
在.net Framework中提供了一个专门用来产生随机数的类System.Random.C#可以用Random产生随机的R.G.B值,从而生成随机的颜色. 对于随机数,计算机不可能产生完全随机的数 ...
- STL之deque用法详解
C++ Deque(双向队列): Deque是一种优化了的.对序列两端元素进行添加和删除操作的基本序列容器.它允许较为快速地随机访问,但它不像vector 把所有的对象保存在一块连续的内存块,而是采用 ...
- Altium Designer 多个输出相连等问题报错解决方法
问题: Altium Designer软件中,项目编译时,Message面板出现,如下错误: Net NetR121_1 contains multiple Output Pins; PC10 con ...
- Linux环境下发布项目(Tomcat重新启动)
在Linux系统下,重启Tomcat 首先,进入Tomcat下的bin目录 cd /usr/local/tomcat/bin 使用Tomcat关闭命令 ./shutdown.sh 查看Tomcat是否 ...