当UITextFiled和UITextView这种文本输入类控件成为第一响应者时,弹出的键盘由他们的一个UIView类的inputView属性来控制,当inputView为nil时会弹出系统的键盘,想要弹出自定义的键盘,将我们自定义的UIView对象给inputView属性赋值即可。表情键盘重点在于排列各个表情和删除键,以及表情键盘上的各种回调设置;
下面为键盘预览图,兼容了竖屏各版本适配,横屏没有兼顾。横屏适配参见这篇博客iOS之自定义表情键盘
图1为6的常用表情,图2为6的全部表情,图3为5的全部表情,表情个数统一为7列3排,根据屏幕不同修改间距以及键盘高度;
下面为项目结构图:采用MVC模式,View层提供表情键盘以及自定义的UITextView。Model层提供表情数据。我为了简单就直接把聊天工具栏通过storyboard拖到了VC上,这里应该再封装一个toolView的;
1、首先来弄好数据层FaceManager,具有一个单例方法、声明了三个数组属性来存放不同的表情;表情图片由Face文件夹来提供;
AllFaces通过我一个名为“emoticons”的plist文件来获取,里面存放的是一个个表情字典,对应着Face中的图片名和图片文字描述;
RecentlyFaces是最近使用过的图片,从本地获取;BigFaces是用来扩展其他大型以及动态效果表情的,没有实现
1 @interface FacesManager : NSObject 2 3 @property (nonatomic, strong, readonly)NSArray * RecentlyFaces; 4 @property (nonatomic, strong, readonly)NSArray * AllFaces; 5 @property (nonatomic, strong, readonly)NSArray * BigFaces; 6 7 + (instancetype)share; 8 - (void)fetchRecentlyFaces; 9 @end 10 11 #import "FacesManager.h" 12 13 @implementation FacesManager 14 15 +(instancetype)share 16 { 17 static FacesManager * m = nil; 18 static dispatch_once_t onceToken; 19 dispatch_once(&onceToken, ^{ 20 m = [[FacesManager alloc] init]; 21 }); 22 return m ; 23 } 24 25 - (instancetype)init 26 { 27 self = [super init]; 28 if (self) { 29 [self fetchAllFaces]; 30 [self fetchBigFaces]; 31 } 32 return self; 33 } 34 35 - (void)fetchAllFaces 36 { 37 NSString * path = [[NSBundle mainBundle] pathForResource:@"emoticons" ofType:@"plist"]; 38 NSArray * arrFace = [NSArray arrayWithContentsOfFile:path]; 39 _AllFaces = arrFace; 40 } 41 42 - (void)fetchRecentlyFaces 43 { 44 NSUserDefaults * defauls = [NSUserDefaults standardUserDefaults]; 45 NSArray * arrFace = [defauls objectForKey:@"RecentlyFaces"]; 46 _RecentlyFaces = arrFace; 47 } 48 49 - (void)fetchBigFaces 50 { 51 52 } 53 54 @end