iOS 自定义数字键盘和计算器

customKeyboard.gif

一、CustomKeyboardView :显示的键盘view
二、keyModel:键盘model,定义键盘字母枚举类型, 存储键盘上btn显示的数据源。
三、KeyboardModeHandler:为处理各种类型键盘数据源,即为MVVM中的ViewModel层。
四、KeyBoardCell:自定义键盘cell,定义键盘操作枚举类型(输入数字、输入小数点、输入运算符等)。
五、WaterFallLayout:键盘的瀑布流。

六、使用
UITextField 和 UITextView 添加自定义键盘

    CustomKeyboardView *keyBoardView = [[CustomKeyboardView alloc] initWithKeyboardType:sender.tag inputSource:_tf];
    keyBoardView.inputText = _tf.text;
     _tf.inputView = keyBoardView;
    keyBoardView.closeKeyboardBlock = ^{
        //TODO
        [_tf resignFirstResponder];
    };
    keyBoardView.confirmBlock = ^{
        //TODO
        [_tf resignFirstResponder];
    };

UISearchBar 添加自定义键盘

UITextField *searchTF = [_searchBar valueForKeyPath:@"_searchField"];
       CustomKeyboardView *keyboardView = [[CustomKeyboardView alloc] initWithKeyboardType:KeyBoardTypeNormal inputSource:searchTF];

    searchTF.inputView = keyboardView;
    keyboardView.closeKeyboardBlock = ^{
       //TODO
        [_searchBar resignFirstResponder];
forState:UIControlStateNormal];
    };
    keyboardView.confirmBlock = ^{
        //TODO
        [_searchBar resignFirstResponder];
    };

七、注意点:记得实现UITextFieldDelegate代理方法

- (BOOL)textField:(UITextField *)textField 
shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    return YES;
}

链接:CustomKeyboardView

参考:https://www.jianshu.com/p/b3e97fb70021

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容