{"id":13696181,"url":"https://github.com/VernonVan/PPStickerKeyboard","last_synced_at":"2025-05-03T16:32:55.160Z","repository":{"id":201274733,"uuid":"117396081","full_name":"VernonVan/PPStickerKeyboard","owner":"VernonVan","description":"iOS 表情键盘","archived":false,"fork":false,"pushed_at":"2018-11-23T09:46:34.000Z","size":1294,"stargazers_count":441,"open_issues_count":8,"forks_count":81,"subscribers_count":16,"default_branch":"master","last_synced_at":"2024-11-13T00:32:37.858Z","etag":null,"topics":["emoji","emotion","ios","keyboard","sticker"],"latest_commit_sha":null,"homepage":"","language":"Objective-C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/VernonVan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2018-01-14T02:41:05.000Z","updated_at":"2024-09-13T09:33:35.000Z","dependencies_parsed_at":"2024-03-28T16:08:18.201Z","dependency_job_id":null,"html_url":"https://github.com/VernonVan/PPStickerKeyboard","commit_stats":null,"previous_names":["vernonvan/ppstickerkeyboard"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VernonVan%2FPPStickerKeyboard","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VernonVan%2FPPStickerKeyboard/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VernonVan%2FPPStickerKeyboard/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VernonVan%2FPPStickerKeyboard/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/VernonVan","download_url":"https://codeload.github.com/VernonVan/PPStickerKeyboard/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252216104,"owners_count":21713099,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["emoji","emotion","ios","keyboard","sticker"],"created_at":"2024-08-02T18:00:37.057Z","updated_at":"2025-05-03T16:32:54.422Z","avatar_url":"https://github.com/VernonVan.png","language":"Objective-C","funding_links":[],"categories":["UI Frameworks"],"sub_categories":[],"readme":"# PPStickerKeyboard\n\n\n![头图](http://upload-images.jianshu.io/upload_images/698554-43916991450f3297.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)\n\n\n\n最近在公司做了个表情键盘的需求，这个需求的技术难度不会很大，比较偏向业务。但是要把用户体验做的好也是不容易的，其中有几个点需要特别注意。话不多说，下面开始正文(注：本文对应的Demo放在Github上：[https://github.com/VernonVan/PPStickerKeyboard](https://github.com/VernonVan/PPStickerKeyboard))。\n\n\n\n## 市面上的表情键盘的分析\n\n首先来看一下市面上主要的几个APP上的表情键盘，平时使用的时候不会去关注细节，这次特意去使用了表情键盘，发现各个APP的体验还是有优有劣的。\n\n首先是QQ和微信，这两者差不多，切换到表情键盘的时候都是没有光标的，这样的用户体验是非常不好的，没有办法在输入表情的时候框选区域，也不能拖动光标进行特定位置的复制黏贴删除等操作，微信甚至在输入框里显示的都不是点击的表情图片，而是文字描述。\n\n![微信QQ表情键盘.JPG](http://upload-images.jianshu.io/upload_images/698554-2c59f42aa0dd0dd1.JPG?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)\n\n\n\n接下来看一下微博国际版，国际版调起表情键盘时是有光标的，是一个\"真正的\"键盘，但是想要拖拽光标的时候，很大概率上会触发到保存图片的行为（如下图所示），导致根本没办法拖动光标。\n![微博国际版误触.JPG](http://upload-images.jianshu.io/upload_images/698554-5f667795a7778a6e.JPG?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)\n\n同时微博国际版输入框表情黏贴后的光标定位是错误的，如下图，开始时光标是在第4个表情后面，然后复制狗头+害羞两个表情黏贴到光标后，光标还是在第4个表情后，同时黏贴的表情前后都莫名多了空格。\n![微博国际版黏贴.JPG](http://upload-images.jianshu.io/upload_images/698554-d4cddecefc22cfc9.JPG?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)\n\n\n\n最后是微博，微博客户端的表情键盘的体验是非常好的，上面说到的问题都不存在，而且表情键盘的删除按钮还能长按删除输入框的内容。\n![微博表情键盘.jpg](http://upload-images.jianshu.io/upload_images/698554-63023d1dfcce132b.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)\n\n## 表情键盘的实现\n\n#### 实现效果\n\n主要实现了以下几个功能\n\n- 能输入表情，有光标，支持复制黏贴删除表情等\n\n- 长按预览表情\n\n- 删除表情、长按连续删除表情\n\n- 适配 iPhone X\n\n  ​\n\n  ![演示.GIF](http://upload-images.jianshu.io/upload_images/698554-9d1661ee627dc3ca.GIF?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)\n\n\n\n#### 基本思路\n\n首先，表情包的图片是用bundle的形式组织的，用`PPSticker`类表征一套表情包，用`PPEmoji`类表征某一个表情，用一个plist作为配置文件，存储表情包的信息。\n![表情的组织.jpg](http://upload-images.jianshu.io/upload_images/698554-10847f03391e9560.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)\n\n`PPStickerDataManager`类主要负责数据部分，用单例的形式，这样可以在初始化的时候只会读取一次plist文件中的所有表情信息；同时我们把输入框内容发到服务端以及从服务端请求到的都是纯文本的，比如会把 \"笑死了🤣\" 转成 \"笑死了[笑哭]\" 这样的纯文本，而不是直接把表情图片直接发到服务端，也就是说项目中有大量的地方会有把文本-\u003e表情的操作，所以`PPStickerDataManager`类也提供匹配某段纯文本中的表情，并把文本替换为图片的功能，`PPStickerDataManager`类的头文件如下：\n\n```objective-c\n@interface PPStickerDataManager : NSObject\n\n+ (instancetype)sharedInstance;\n\n/// 所有的表情包\n@property (nonatomic, strong, readonly) NSArray\u003cPPSticker *\u003e *allStickers;\n\n/* 匹配给定attributedString中的所有emoji，如果匹配到的emoji有本地图片的话会直接换成本地的图片\n *\n * @param attributedString 可能包含表情包的attributedString\n * @param font 表情图片的对齐字体大小\n */\n- (void)replaceEmojiForAttributedString:(NSMutableAttributedString *)attributedString font:(UIFont *)font;\n\n@end\n```\n\n\n\n#### \"真正的\"键盘\n\n真正的键盘也就是说调起表情键盘时输入框是有光标的，能进行拖拽光标、选中区域等的操作，这样的体验才是与系统键盘一致的。其实系统已经提供好了接口给我们直接使用，`UITextView`和`UITextField`都有的`inputView`和`inputAccessoryView`就是用来实现自定义键盘的，这两个属性的定义如下：\n\n```objective-c\n// Presented when object becomes first responder.  If set to nil, reverts to following responder chain.  If\n// set while first responder, will not take effect until reloadInputViews is called.\n@property (nullable, readwrite, strong) UIView *inputView;             \n@property (nullable, readwrite, strong) UIView *inputAccessoryView;\n```\n\n同时系统键盘在 设置-\u003e声音-\u003e按键音 选项打开且手机非静音状态下输入是有按键的声音的，这个按键音也是可以支持的，只要自定义键盘类遵循`UIInputViewAudioFeedback`协议，同时实现 `enableInputClicksWhenVisible`方法并返回YES，这样就可以在点击表情的时候调用`[[UIDevice currentDevice] playInputClick]`方法发出按键音了，详情请查看[苹果的官方文档](https://developer.apple.com/library/content/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/InputViews/InputViews.html)。\n\n下面是Demo中键盘切换方法的实现：\n\n```objective-c\n- (void)changeKeyboardTo:(PPKeyboardType)toType\n{\n    switch (toType) {\n        case PPKeyboardTypeSystem:\n            self.textView.inputView = nil;    // 切换到系统键盘\n            [self.textView reloadInputViews]; // 调用reloadInputViews方法会立刻进行键盘的切换\n            break;\n        case PPKeyboardTypeSticker:            \n            self.textView.inputView = self.stickerKeyboard; // 切换到自定义的表情键盘\n            [self.textView reloadInputViews];\n            break;\n        default:\n            break;\n    }\n}\n```\n\n\n\n#### 去除表情的拖拽交互\n\n在iOS11上，`UITextView`上的`NSTextAttachment`（表情）默认可以进行拖拽交互，但是却导致拖动光标时很容易触发这个交互（图示可以查看上面说到的微博国际版中的误触）。一番查找之后才找到一个比较隐蔽的属性：`textDragInteraction`，直接设置为`NO`就能禁止掉`NSTextAttachment`的拖拽交互。\n\n```objective-c\nif (@available(iOS 11.0, *)) {\t// 只在iOS11及以上才有这个属性\n     _textView.textDragInteraction.enabled = NO;\n}\n```\n\n\n\n#### 与服务端的交互\n\n我们在输入框中输入的内容与服务端进行交互的时候都是用纯文本的，比如会把 \"笑死了🤣\" 转成 \"笑死了[笑哭]\" 这样的纯文本发到服务端，而不是直接发表情图片，向服务端请求内容的时候也是传回 \"笑死了[笑哭]\"，然后客户端再根据正则匹配找出表情替换成对应的表情图片，然后显示到页面上。具体过程可以看下图：\n![与服务端的交互.png](http://upload-images.jianshu.io/upload_images/698554-00199a31ec46945d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)\n\n\n\n也就是说，我们设置到输入框的`NSAttributedString`中的每一个`NSTextAttachment`都有一个\"隐藏的\"属性—表情的文本描述，这里对`NSAttributedString`进行拓展就能实现。`pp_setTextBackedString`可以对`NSAttributedString`的指定`range`设置一个`PPTextBackedString`类型的属性，而`pp_plainTextForRange`能拿到`NSAttributedString`指定`range`的纯文本。具体实现如下：\n\n```objective-c\n@implementation NSAttributedString (PPAddition)\n\n- (NSString *)pp_plainTextForRange:(NSRange)range\n{\n    if (range.location == NSNotFound || range.length == NSNotFound) {\n        return nil;\n    }\n\n    NSMutableString *result = [[NSMutableString alloc] init];\n    if (range.length == 0) {\n        return result;\n    }\n\n    NSString *string = self.string;\n    [self enumerateAttribute:PPTextBackedStringAttributeName inRange:range options:kNilOptions usingBlock:^(id value, NSRange range, BOOL *stop) {\n        PPTextBackedString *backed = value;\n        if (backed \u0026\u0026 backed.string) {\n            [result appendString:backed.string];\n        } else {\n            [result appendString:[string substringWithRange:range]];\n        }\n    }];\n    return result;\n}\n\n@end\n\n@implementation NSMutableAttributedString (PPAddition)\n\n- (void)pp_setTextBackedString:(PPTextBackedString *)textBackedString range:(NSRange)range\n{\n    if (textBackedString \u0026\u0026 ![NSNull isEqual:textBackedString]) {\n        [self addAttribute:PPTextBackedStringAttributeName value:textBackedString range:range];\n    } else {\n        [self removeAttribute:PPTextBackedStringAttributeName range:range];\n    }\n}\n\n@end\n```\n\n\n\n#### 灵活的光标\n\n表情功能，`UITextView`都是用`NSAttributedString`进行赋值的，并且我们底层其实还是用上面说到的纯文本进行实现的，那么把 [笑死] 转成 🤣 就会从4个字符变成1个字符，这里是有差值的，如果不处理的话就会出现上面提到的微博国际版中复制黏贴输入框的表情会导致光标位置不对，甚至莫名其妙多出前后空格的问题。为了精准的定位光标，我们需要自行处理好这些问题。\n\n这里自己继承并实现了`UITextView`的子类`PPStickerTextView`，在这个类中重载复制、黏贴、剪切等操作，分别对应的方法如下：\n\n```objective-c\n- (void)cut:(id)sender;\t\t// 剪切\n\n- (void)copy:(id)sender;\t// 复制\n\n- (void)paste:(id)sender;\t// 黏贴\n```\n\n下面以剪切方法举例，看看怎么处理光标的问题，需要注意的地方请看对应的注释：\n\n```objective-c\n- (void)cut:(id)sender\n{\n    // 1.从textView中拿到对应的纯文本，比如：笑死了[笑死]\n    NSString *string = [self.attributedText pp_plainTextForRange:self.selectedRange];\n    if (string.length) {\n      \t// 2. 将纯文本写入到剪贴板中\n        [UIPasteboard generalPasteboard].string = string;\n\n      \t// 3. 记住当前的光标位置\n        NSRange selectedRange = self.selectedRange;\n        NSMutableAttributedString *attributeContent = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];\n      \t// 4. 将检测到是表情的文本替换成对应的图片\n        [attributeContent replaceCharactersInRange:self.selectedRange withString:@\"\"];\n        self.attributedText = attributeContent;\n      \n      \t// 5. 重新设置光标\n        self.selectedRange = NSMakeRange(selectedRange.location, 0);\n    }\n}\n```\n\n技术点的分析就是以上这些，详细的代码可以clone代码查看：https://github.com/VernonVan/PPStickerKeyboard","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FVernonVan%2FPPStickerKeyboard","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FVernonVan%2FPPStickerKeyboard","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FVernonVan%2FPPStickerKeyboard/lists"}