自定制emoji替换系统的emoji键盘
一、关于emoji表情
随着iOS系统版本的升级,对原生emoji表情的支持也越来越丰富。emoji表情是unicode码中为表情符号设计的一组编码,当然,还有独立于unicode的另一套编码SBUnicode,在OS系统中,这两种编码都有很好的支持。UI系统会自动帮我们将编码转义成表情符号,例如用SBUnicode如下代码:
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
label.font = [UIFont systemFontOfSize:25];
label.text = @"\uE056";
[self.view addSubview:label];
就会在屏幕上出现一个笑脸:

二、开发表情键盘的思路
首先为了实现跨平台,无论iOS端,andorid端还是web端,都要有一个相同的标准,这个标准就可以是国际Unicode编码,我们的思路是将表情文字进行unicode编码后再进行传输,因此,有两中方式,一种是通过自定义一套表情切图,将其与unicode码一一对应,在转码的时候,我们一一遍历,转换成unicode后进行传输,这样的好处是我们可以保证所有平台所能使用的表情统一。在iOS端,可以有另一种方式,通过上面我们知道,通过SBUnicode码我们可以在客户端显示表情符号,并且这个码的排列是十分有规律的,通过这个特点,我们可以通过遍历SBUnicode码的范围进行表情的创建,省去的图片素材的麻烦。
iOS中可用的表情unicode范围是:0xE001~0xE05A,0xE101~0xE15A,
0xE201~0xE253,0xE401~0xE44C,0xE501~0xE537。
我们可以通过遍历的方法,将其都加入数据源数组中:
int emojiRangeArray[10] = {0xE001,0xE05A,0xE101,0xE15A,0xE201,0xE253,0xE401,0xE44C,0xE501,0xE537};
for (int j = 0 ; j<10 ; j+=2 ) {
int startIndex = emojiRangeArray[j];
int endIndex = emojiRangeArray[j+1];
for (int i = startIndex ; i<= endIndex ; i++ ) {
//添加到数据源数组
[dataArray addObject:[NSString stringWithFormat:@"%C", (unichar)i]];
}
}
键盘的摆放,可以通过collectionView来做,十分方便:
//为了摆放分页控制器,创建一个背景view
bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200)];
//分页控制器
pageControlBottom = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 170, [UIScreen mainScreen].bounds.size.width, 20)];
[bgView addSubview:pageControlBottom];
//collectionView布局
UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
//水平布局
layout.scrollDirection=UICollectionViewScrollDirectionHorizontal;
//设置每个表情按钮的大小为30*30
layout.itemSize=CGSizeMake(30, 30);
//计算每个分区的左右边距
float xOffset = (kscreenWidth-7*30-10*6)/2;
//设置分区的内容偏移
layout.sectionInset=UIEdgeInsetsMake(10, xOffset, 10, xOffset);
scrollView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 160) collectionViewLayout:layout];
//打开分页效果
scrollView.pagingEnabled = YES;
//设置行列间距
layout.minimumLineSpacing=10;
layout.minimumInteritemSpacing=5;
scrollView.delegate=self;
scrollView.dataSource=self;
scrollView.backgroundColor = bgView.backgroundColor;
[bgView addSubview:scrollView];
在collectionView的回调方法中,处理如下:
//每页28个表情
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
if (((dataArray.count/28)+(dataArray.count%28==0?0:1))!=section+1) {
return 28;
}else{
return dataArray.count-28*((dataArray.count/28)+(dataArray.count%28==0?0:1)-1);
}
}
//返回页数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return (dataArray.count/28)+(dataArray.count%28==0?0:1);
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"biaoqing" forIndexPath:indexPath];
for (int i=cell.contentView.subviews.count; i>0; i--) {
[cell.contentView.subviews[i-1] removeFromSuperview];
}
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
label.font = [UIFont systemFontOfSize:25];
label.text =dataArray[indexPath.row+indexPath.section*28] ;
[cell.contentView addSubview:label];
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSString * str = dataArray[indexPath.section*28+indexPath.row];
//这里手动将表情符号添加到textField上
}
//翻页后对分页控制器进行更新
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat contenOffset = scrollView.contentOffset.x;
int page = contenOffset/scrollView.frame.size.width+((int)contenOffset%(int)scrollView.frame.size.width==0?0:1);
pageControlBottom.currentPage = page;
}
三、切换系统键盘和自定义的表情键盘
UITextField和UITextView都会有下面这个属性和方法:
@property (nullable, readwrite, strong) UIView *inputView;
- (void)reloadInputViews;
inputView我们可以设置textView和textField成为第一响应时的弹出附件,如果我们不设置或者设置为nil,则会弹出系统键盘,reloadInputView方法可以使我们刷新这个附件视图,通过这两个,我们可以非常轻松的实现键盘的切换,比如我们在一个出发方法中如下处理:
-(void)imageViewTap{
if (![_publishContent isFirstResponder]) {
return;
}
if (isEmoji==NO) {
isEmoji=YES;
//呼出表情
_textView.inputView=bgView;
[_textView reloadInputViews];
}else{
isEmoji=NO;
_textView.inputView=nil;
[_textView reloadInputViews];
}
}
效果如下:


追注:测试上面的SBUnicode码在模拟器上可以正常显示,真机并不能识别,可以通过将表情符全部添加到一个plist文件中,通过文件读取来创建键盘的方式进行真机上的开发。plist文件地址如下:
------------------------------------------分割线------------------------------------------
免费下载地址在 http://linux.linuxidc.com/
用户名与密码都是www.linuxidc.com
具体下载目录在 /2015年资料/12月/12日/iOS自定义的emoji表情键盘/
下载方法见 http://www.linuxidc.com/Linux/2013-07/87684.htm
------------------------------------------分割线------------------------------------------
本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-12/126157.htm
自定制emoji替换系统的emoji键盘的更多相关文章
- 【Cocos2d-x】屏蔽Emoji并解决由于Emoji导致的崩溃问题
IOS的Emoji表情因为编码问题,在Android手机上无法正常显示,如果当前的cc.Label节点使用的是系统字,在系统字库中找不到对应编码的字符,会导致崩溃. 为了解决这个问题,又要兼顾新老版本 ...
- Windows 7 封装篇(一)【母盘定制】[手动制作]定制合适的系统母盘
Windows 7 封装篇(一)[母盘定制][手动制作]定制合适的系统母盘 http://www.win10u.com/article/html/10.html Windows 7 封装篇(一)[母盘 ...
- 介绍一种很棒的wince 如何替换系统声音的方法
Topic:介绍一种很棒的wince 如何替换系统声音的方法(作者:Baiduluckyboy) //------------------------------------------------- ...
- 替换系统数据库解决SQLSERVER服务启动不了的问题
替换系统数据库解决SQLSERVER服务启动不了的问题 当遇到SQLSERVER服务启动不起来的时候,我们试过把系统的四个数据库master ,model ,tempdb,msdb 替换掉,Windo ...
- 定制x86 Linux系统
/************************************************************************************* * 定制x86 Linux ...
- android使用mount挂载/system/app为读写权限,删除或替换系统应用
注意:以下代码中#开头的则为需要执行的shell命令,其他的为打印的结果.#代表需要使用ROOT权限(su)执行,所以想要修改您android手机某个目录挂载为读写,首先需要有ROOT权限! 先要得到 ...
- iOS之自定义UITabBar替换系统默认的(添加“+”号按钮)
自定义UITabBar替换系统默认的,目的是为了在UITabBar中间位置添加一个“+号按钮”,下面我们来聊聊具体的实现. 1.自定义WBTabBar,让其继承自UITabBar,代码如下: // / ...
- 深入浅出 - Android系统移植与平台开发(十)- Android编译系统与定制Android平台系统(瘋耔修改篇二)
第四章.Android编译系统与定制Android平台系统 4.1Android编译系统 Android的源码由几十万个文件构成,这些文件之间有的相互依赖,有的又相互独立,它们按功能或类型又被放到不同 ...
- iOS自定制tabbar与系统的tabbar冲突,造成第一次点击各个item图片更换选中,第二次选中部分item图片不改变
可以选择是使用自定制的还是系统的,如果使用自定制的,就使用以下方法即可隐藏系统的uitabbarButton,从而使item恢复正确 //隐藏UITabBarButton -(void)viewWil ...
随机推荐
- Database SQL script automation management tools investigation
Recently researched about database SQL scripts auto management tools, recorded the results here. Res ...
- Threads and Anonymous Classes in JAVA
As we all know,a thread is a separate process on your computer.you can run multiple threads all at t ...
- C#中析构函数,命名空间及字符串的运用(Ninth day)
又到了总结知识的时间了,今天在云和学院学习了析构函数,命名空间及字符串的处理,现在就为大家总结下来. 理论: 析构函数 不能在结构中定义析构函数.只能对类使用析构函数. 一个类只能有一个析构函数. 无 ...
- Extjs 3.0 htmleditor实现插入图片功能
首先感谢前辈们的无私奉献.贴出参考地址 http://zhidao.baidu.com/link?url=Q0ZM405OFNy_xAHSut9TepRJxgXCxFayQttrQz1N82dlA1_ ...
- java反射机制入门3
Method对象的机制与实现 1.Method对象概述 1)java.lang.reflect.Method类是用于表示类中.接口中方法对象的类. 2)可以操作类中私有,以及公有等全部方法. 2.Me ...
- Mac 终端命令运行java
链接地址:http://www.cnblogs.com/wangrui-techbolg/archive/2012/12/29/2839047.html 由于mac已经装好java环境,所以直接课运行 ...
- java代码获取ip地址
public class IpTool { public static void main(String[] args) { IpTool ipTool=new IpTool(); System.ou ...
- TortoiseSVN和Eclipse中subversion无法协同使用
环境: Eclipse版本Luna, 第一次安装subversion插件时, 使用了http://download.eclipse.org/releases/luna中的Subversive, 版本为 ...
- python命令行解析工具argparse模块【5】
上一节我们学习了parse_args()的用法,这一节,我们将继续学习argparse的其他一些用法. 1.sub-commands子命令 argpar ...
- jquery事件链式写法
<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...