我们在做iOS开发的时候肯定会遇到这样需求,比如在做聊天功能的时候要自己定制表情键盘啊,或者需要制作一个特殊符号的键盘,等等。如下图:
那么这个时候我们该如何去做呢?
如何定制键盘的UI
首先这个键盘肯定是一个UIView或者UIViewController(iOS8之后支持),至于是什么样子就跟你们自己的项目需求去写了。
一般在iOS里面的输入框有UITextField,或者UITextView,他们都继承于UIResonder(任何UIView都继承与UIResponder),在根据UIResponder的文档,他有为inputView(也可以理解为键盘)提供如下的接口,这里附上官方的文档。
@interface UIResponder (UIResponderInputViewAdditions)
// Called and presented when object becomes first responder. Goes up the responder chain.
@property (nullable, nonatomic, readonly, strong) __kindof UIView *inputView NS_AVAILABLE_IOS(3_2);
@property (nullable, nonatomic, readonly, strong) __kindof UIView *inputAccessoryView NS_AVAILABLE_IOS(3_2);
/// This method is for clients that wish to put buttons on the Shortcuts Bar, shown on top of the keyboard.
/// You may modify the returned inputAssistantItem to add to or replace the existing items on the bar.
/// Modifications made to the returned UITextInputAssistantItem are reflected automatically.
/// This method should not be overriden. Goes up the responder chain.
@property (nonnull, nonatomic, readonly, strong) UITextInputAssistantItem *inputAssistantItem NS_AVAILABLE_IOS(9_0) __TVOS_PROHIBITED __WATCHOS_PROHIBITED;
// For viewController equivalents of -inputView and -inputAccessoryView
// Called and presented when object becomes first responder. Goes up the responder chain.
@property (nullable, nonatomic, readonly, strong) UIInputViewController *inputViewController NS_AVAILABLE_IOS(8_0);
@property (nullable, nonatomic, readonly, strong) UIInputViewController *inputAccessoryViewController NS_AVAILABLE_IOS(8_0);
/* When queried, returns the current UITextInputMode, from which the keyboard language can be determined.
* When overridden it should return a previously-queried UITextInputMode object, which will attempt to be
* set inside that app, but not persistently affect the user's system-wide keyboard settings. */
@property (nullable, nonatomic, readonly, strong) UITextInputMode *textInputMode NS_AVAILABLE_IOS(7_0);
/* When the first responder changes and an identifier is queried, the system will establish a context to
* track the textInputMode automatically. The system will save and restore the state of that context to
* the user defaults via the app identifier. Use of -textInputMode above will supercede use of -textInputContextIdentifier. */
@property (nullable, nonatomic, readonly, strong) NSString *textInputContextIdentifier NS_AVAILABLE_IOS(7_0);
// This call is to remove stored app identifier state that is no longer needed.
+ (void)clearTextInputContextIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(7_0);
// If called while object is first responder, reloads inputView, inputAccessoryView, and textInputMode. Otherwise ignored.
- (void)reloadInputViews NS_AVAILABLE_IOS(3_2);
@end
从文档中可以看到可以设置inputView和inputViewController,这个时候你还是不能将你自己的键盘添加进去,因为inputView和inputViewController是readonly的,无法赋值。不过这个很好办,你可以自己把他开放出来,如下面代码所示
@interface UIResponder (WriteableInputView)
@property (nullable, nonatomic, readwrite, strong) __kindof UIView *inputView;
@property (nullable, nonatomic, readwrite, strong) UIInputViewController *inputViewController;
@end
需要注意的是inputViewController是UIInputViewController类型的,想了解更多可以查看UIInputViewController文档,因为特别简单这里就不再解释。还需要注意的是这个是iOS8以后的新接口,如果你的软件兼容iOS8之前的版本,就需要注意了。
如何把键盘的内容输入到文本框里
一般在考虑到给文本框输入字符串,大多数程序员都会用inputView.text == keyboardString;这种方法也可以实现,但是这并不是正确的方法。
查看文档你会发现,文本输入框都遵循一个协议 UITextInput (这个协议太长,大家自己看文档吧��)。
在给输入框赋值的之后我建议用这个方法
@protocol UITextInput <UIKeyInput>
- (void)replaceRange:(UITextRange *)range withText:(NSString *)text;
大概的用法就是
[self.textInput replaceRange:self.textInput.selectedTextRange withText:text]
例子
如何自定义一个键盘大概的思路就是这样子了。我自己也抽时间写了一个开源代码MPEmojiKeyboard
MPEmojiKeyboard是一个表情键盘,可以定制各种表情,可以用在各种聊天软件里面。
这个项目写的很粗糙,各位看了有什么想法,欢迎看了各种修改。然后也希望大家能够多多star,我会继续写更多的开源代码给大家!
在iOS开发中,为了满足特定需求,如聊天应用的表情键盘,需要自定义键盘UI。通过设置inputView和inputViewController,可以创建自定义键盘。对于键盘内容输入文本框,应遵循UITextInput协议,使用insertText:方法。文中还推荐了一个开源项目MPEmojiKeyboard,用于创建可定制的表情键盘。
652

被折叠的 条评论
为什么被折叠?



