1.自定义一个表情包类继承NSTextAttachment

 #import <UIKit/UIKit.h>

 /** 表情包的自定义类*/
@interface EmojiTextAttach : NSTextAttachment @property (nonatomic,assign) NSRange range; /** 绑定NSTextAttachment所表示的表情和与其对应的标志*/
@property (nonatomic, copy) NSString *emojiTag; /** 表情名称*/
@property (nonatomic, copy) NSString *emojiName; @end

2.每个emoji表情其实就是一张图片,并且每张图片都有统一的编号,也就是说一个emoji

3.将emoji的图片赋值给自定义的表情包类image属性,并且将emoji转为富文本

 #pragma mark - 选择了emoji表情
-(void)emojiView:(FacialView *)emojiView didSelectEmoji:(NSString *)emoji withEmojiPng:(UIImage *)emojiPng
{
EmojiTextAttach *emojiAttch = [[EmojiTextAttach alloc] init];
emojiAttch.image = emojiPng;
emojiAttch.emojiTag = emoji;
// 当前插入的富文本
NSAttributedString *addEmoji = [NSAttributedString attributedStringWithAttachment:emojiAttch]; // 输入框所有的可变富文本
NSMutableAttributedString *allText = [[NSMutableAttributedString alloc] initWithAttributedString:_textView.attributedText];
NSLog(@"allText-->%@",allText); // 将选择的表情插入到输入框
[allText insertAttributedString:addEmoji atIndex:_textView.selectedRange.location]; [allText addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18.0] range:NSMakeRange(, allText.length)]; _textView.attributedText = allText; NSLog(@"allText-->%@",allText);
//光标位置
_textView.selectedRange = NSMakeRange(_textView.selectedRange.location + , ); //[_textView setText:[_textView.text stringByAppendingString:emoji]]; [self textViewDidChange:_textView];
}

4.发送时将富文本转为对应图片编号的纯文本发送给服务器

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{ if ([text hasSuffix:@"@"]) { //输入过程 if ([self.delegate respondsToSelector:@selector(showGroupMemberVC)]) {
[self.delegate showGroupMemberVC];
}
} if (_chatAuth) {
if ([_chatAuth isEqual:@"noAuth"]) { [ErrorMessage showErrorMessage:@"您暂时没有发言权限!" inView:[UIApplication sharedApplication].keyWindow];
return NO;
}else if ([_chatAuth isEqual:@"isSaid"]) {
//[ErrorMessage showErrorMessage:@"您今天已经发过言了,明天再来吧!" inView:[UIApplication sharedApplication].keyWindow];
//return NO;
}else{ }
} // 得到纯文本发送
NSString *attrStr = textView.attributedText.getPlainString; if (![attrStr isEqual:@""]) {
if ([text isEqual:@"\n"]) { NSString * headerData = [CommonTool removeHeaderOrTrailSpaceWithContent:attrStr]; if ([self messageLengthToolong:headerData]) {
[ErrorMessage showErrorMessage:@"消息内容过长!" inView:_superView];
return NO;
} if (![headerData isEqual:@""]) {
if ([self.delegate respondsToSelector:@selector(sendMessage:)]) {
[self.delegate sendMessage:headerData];
}
}else{
[ErrorMessage showErrorMessage:@"消息不能为空!" inView:_superView];
} textView.text = @"";
[self recoverInputTextViewFrame:textView]; return NO;
}
}
return YES;
}

【iOS开发】emoji表情的输入思路的更多相关文章

  1. iOS 获取emoji表情和拦截emoji表情

      1 2 //将数字转为 #define EMOJI_CODE_TO_SYMBOL(x) ((((0x808080F0 | (x & 0x3F000) >> 4) | (x &a ...

  2. 手机自带输入法emoji表情的输入,提交及显示——纯前端解决方案

    很早之前就遇到过需要前端支持用户输入并提交emoji表情的问题,一直没有尝试去解决,今天再一次狭路相逢,该来的躲不过,那就着手解决吧. 大多数emoji表情都是4字节的utf-16编码(辅助平面字符, ...

  3. 最全最详细的用JS过滤Emoji表情的输入

    在前端页面开发过程中,总会碰到不允许输入框输入emoji表情的需求,我的思路是通过编码用正则匹配表情,然后将其替换为空字符创.但是问题也是显而易见的,完整的编码集是什么呢?查阅了官方文档,发现上面并没 ...

  4. 手机自带输入法emoji表情的输入,提交及显示——前端解决方案

    体验更优排版请移步原文:http://blog.kwin.wang/programming/emoji-transform-commit.html 之前就遇到过需要前端支持用户输入并提交emoji表情 ...

  5. 用JS过滤Emoji表情的输入

    本文为原创,转载请注明出处: cnzt       文章:cnzt-p http://www.cnblogs.com/zt-blog/p/6773854.html 在前端页面开发过程中,总会碰到不允许 ...

  6. iOS 自定义emoji表情键盘

    之前走了很多弯路,包括自己定以emoji表情,自己创建view类去处理图文混排 ,当把这些焦头烂额的东西处理完了才发现 ,其实系统自带键盘是如此的方便,iOS 系统自带的表情在view,textfie ...

  7. iOStextField/textView在输入时限制emoji表情的输入

    https://www.jianshu.com/p/5227e6aab4d4 2017.02.27 13:08* 字数 146 阅读 6109评论 6喜欢 14 又遇到输入框输入表情的情况了,之前写了 ...

  8. mysql支持IOS的Emoji表情

    原因: UTF-8编码有可能是两个.三个.四个字节.Emoji表情是4个字节,而Mysql的utf8编码最多3个字节,所以数据插不进去. 解决办法: 将Mysql的编码从utf8转换成utf8mb4 ...

  9. Js 过滤emoji表情...持续补充中..

    原文来自: https://www.cnblogs.com/tsjTSJ/p/7065544.html 最全最详细的用JS过滤Emoji表情的输入   在前端页面开发过程中,总会碰到不允许输入框输入e ...

随机推荐

  1. [LeetCode OJ] Reorder List—Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…

    For example,Given {1,2,3,4}, reorder it to {1,4,2,3}. /** * Definition for singly-linked list. * str ...

  2. 页面添加 mask 遮罩层

    var mask = function(){ $('<div>').css({ position: 'fixed', left: 0, top: 0, width: '100%', hei ...

  3. CentOS6.5安装LAMP环境APACHE的安装

    1.卸载apr.apr-util [root@centos6 LAMP]# yum remove apr apr-util 2.编译安装apr-1.5.1.tar.gz [root@centos6 L ...

  4. 通过 Xshell 5 连接 centOS 7 服务器

    一. 在安装好了centOS 7 的服务上,打开终端 运行 ip -s addr 命令 获取服务的IP地址 [root@localhost ~]# ip -s addr1: lo: <LOOPB ...

  5. MYSQL一对多,两表查询合并数据

    select a.askid,a.title,GROUP_CONCAT(b.message SEPARATOR '----') as content from gg_ask as a join gg_ ...

  6. Forms & HTML 组件 - laravelcollective/html

    简书链接 :Forms & HTML 组件 - laravelcollective/html 安装 方法一: composer require laravelcollective/html 方 ...

  7. poj Organize Your Train part II

    http://poj.org/problem?id=3007 #include<cstdio> #include<algorithm> #include<cstring& ...

  8. POJ2996 Help Me with the Game(模拟)

    题目链接. 分析: 简单模拟. #include <iostream> #include <queue> #include <cstdio> #include &l ...

  9. 【动态规划】HDU 1081 & XMU 1031 To the Max

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1081 http://acm.xmu.edu.cn/JudgeOnline/problem.php?i ...

  10. datagridview的数据源的操作

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...