新浪微博客户端(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算是初步入门了吧,刚好最近对微博感兴趣就 ...
随机推荐
- pycharm常用快捷键总结
http://blog.csdn.net/pipisorry/article/details/39909057 在PyCharm /opt/pycharm-3.4.1/help目录下可以找到Refer ...
- 自定义多列排序:C++/Java实现
前言: 有些时候,我们在编程中会遇到多列排序的需求.假如在execle,这事儿就太easy了.不过没办法,现在就需要你用Java或者C++实现这样一个功能! 比如将下表无序的数据通过重排之后按照以下规 ...
- 【bzoj2038】 小Z的袜子(hose)
http://www.lydsy.com/JudgeOnline/problem.php?id=2038 (题目链接) 转自:http://blog.csdn.net/bossup/article/d ...
- Gulp解决发布线上文件(CSS和JS)缓存问题
Gulp解决发布线上文件(CSS和JS)缓存问题 本文的缘由:目前经常线上发布文件后要不断的刷新页面及过很长时间,页面上的CSS和JS文件才能生效,特别对于目前做微信商城的时候,微信内置的浏览器缓存非 ...
- 64位CentOS源码编译方式安装wine
说明:本文仅作本人笔记的之用,仅供参考.可能因不同环境而不同. 1. 从官网下载最新版的wine-1.6.2.tar.gz 2. 安装相关的包(这里是我安装的,可能由于不同系统已经安装的包不同而不一样 ...
- Unable to execute dex: java.nio.BufferOverflowException.解决办法
异常提示: [2014-01-16 09:27:35 - Dex Loader] Unable to execute dex: java.nio.BufferOverflowException. Ch ...
- Linux rsync网站目录同步功能的实现
实现目标: 172.16.1.64服务器上的/var/www/sw_service目录,与172.16.1.60服务器上的/var/www/sw_service目录实现同步, 即1.60主动向1.64 ...
- Linux 中 17 个 tar 命令实用示例
Tar(Tape ARchive,磁带归档的缩写,LCTT 译注:最初设计用于将文件打包到磁带上,现在我们大都使用它来实现备份某个分区或者某些重要的目录)是类 Unix 系统中使用最广泛的命令,用于归 ...
- 各种编码UNICODE、UTF-8、ASCII学习笔记
本文转自csdn博客:http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf-8.html ,感谢作者的分享 作者: 阮一峰 日期: ...
- sql练习