之前项目中的表情键盘,现在和大家分享一下吧,写的不好,不喜勿喷!
项目地址:EmotionKeyboard
效果图

直接把文件中的EmotionKeyboard文件添加到项目中即可,本项目依赖 MJExtension,需要注意的是项目资源文件直接 Create folder reference 形式引入.

使用方法
提供4种创建方法
1 2 3 4 5 6 7
| EmotionKeyboard *emotionKeyboard = [[EmotionKeyboard alloc]initWithFrame:CGRectMake(0, ScreenH - 235, ScreenW, 235)];
EmotionKeyboard *emotionKeyboard = [[EmotionKeyboard alloc]init];
EmotionKeyboard *emotionKeyboard = [[EmotionKeyboard alloc]initWithDefault];
EmotionKeyboard *emotionKeyboard = [EmotionKeyboard sharedEmotionKeyboardView];
|
initWithFrame: 可自己指定键盘大小 已经做适配 默认为有四个选项
init: 默认Frame为 CGRectMake(0, ScreenH - 216, ScreenW, 216) 默认为有四个选项
sharedEmotionKeyboardView:单利模式 默认Frame为 CGRectMake(0, ScreenH - 216, ScreenW, 216) 默认为有四个选项
initWithDefault: 默认Frame为 CGRectMake(0, ScreenH - 216, ScreenW, 216) 只有默认的小表情

默认创建大小自己可修改
接口
本项目是代理对外传值,只需成为代理 主要代理方法如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| /** * 获取图片对应文字 * @param text 文字 */ - (void)emoticonInputDidTapText:(NSString *)text;
/** * 获取图片表情对应的url * @param url 图片路径 */ - (void)emoticonImageDidTapUrl:(NSString *)url;
/** * 删除表情按键点击 */ - (void)emoticonInputDidTapBackspace; /** * 发送表情按钮点击 */ - (void)emoticonInputDidTapSend;
|
扩展接口
扩展接口都放到EmotionTool 文件中
添加图片到收藏
1 2 3 4 5 6 7 8
| /** * 添加图片到收藏 * @param url 图片url */ + (void)addCollectImage:(NSString *)url;
//使用 [EmotionTool addCollectImage:url];
|
如果需要替换默认表情,请替换资源文件. 并按照对应要去修改资源目录下info.plist文件
默认素材来自于新浪微博
具体使用方法 –参考demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(10, 200, 300, 44)]; textField.backgroundColor = [UIColor lightGrayColor];
// EmotionKeyboard *emotionKeyBoard = [[EmotionKeyboard alloc]initWithFrame:CGRectMake(0, ScreenH - 235, ScreenW, 235)]; // EmotionKeyboard *emotionKeyBoard = [[EmotionKeyboard alloc]init]; // EmotionKeyboard *emotionKeyBoard = [[EmotionKeyboard alloc]initWithDefault]; EmotionKeyboard *emotionKeyBoard = [EmotionKeyboard sharedEmotionKeyboardView]; emotionKeyBoard.delegate = self; textField.inputView = emotionKeyBoard; _textField = textField;
// FaceBoardDelegateMethods
/** * 删除表情按钮点击 */ - (void)emoticonInputDidTapBackspace { NSString* inputString; inputString = _textField.text; NSString* string = nil; NSInteger stringLength = inputString.length; if (stringLength > 0) { if (stringLength == 1 || stringLength == 2) {//只有1个或2个字符时 if ([inputString isEmoji]) {//emoji string = @""; }else {//普通字符 string = [inputString substringToIndex:stringLength - 1]; } }else if ([@"]" isEqualToString:[inputString substringFromIndex:stringLength - 1]]) {//默认表情 if ([inputString rangeOfString:@"["].location == NSNotFound) { string = [inputString substringToIndex:stringLength - 1]; }else { string = [inputString substringToIndex:[inputString rangeOfString:@"["options:NSBackwardsSearch].location]; } }else if ([[inputString substringFromIndex:stringLength - 1] isEmoji] || [[inputString substringFromIndex:stringLength - 2] isEmoji]) {//末尾是emoji if ([[inputString substringFromIndex:stringLength - 2] isEmoji]) { string = [inputString substringToIndex:stringLength - 2]; }else if ([[inputString substringFromIndex:stringLength - 1] isEmoji]) { string = [inputString substringToIndex:stringLength - 1]; } }else {//是普通文字 string = [inputString substringToIndex:stringLength - 1]; } } [_textField setText:string]; }
/** * 发送表情按钮点击 */ - (void)emoticonInputDidTapSend { NSLog(@"发送"); // [self sendComment]; }
/** * 获取表情对应字符 * * @param text 表情对应字符串 */ - (void)emoticonInputDidTapText:(NSString*)text { [_textField insertText:text]; } /** * 获取图片表情对应的url * * @param url */ - (void)emoticonImageDidTapUrl:(NSString *)url{ NSLog(@"%@",url); }
|