Code Monkey home page Code Monkey logo

ptdiagitalkeyboard's Introduction

PTDiagitalKeyBoard

iOS系统提供了多种键盘,我们可以通过Enum类型设置。但有的时候由于某些特殊业务的需要,我们不得不自定义键盘。

说到自定义键盘,就得说起inputViewinputAccessoryView了。

inputView是键盘的主体部分,inputAccessoryView则相当于键盘上的工具栏。

UITextInput 协议

键盘构建的重点在于将输出的内容显示到输入框中。由于输入框都遵循了<UITextInput>协议,所以只需要根据协议来获取输入框相关状态、改变输入框的文本便可。

这里我们通过UITextInput协议参考,找到对应的协议方法,来对输入框文本做一些简单的更改。

inputView

//退格
- (void)event_delete
{
    UIResponder <UITextInput>*firstResponse = (id)self.nextResponder;

    [firstResponse deleteBackward];
}

// 添加字符串
- (void)event_addCharacter:(NSString *)string
{
    UIResponder <UITextInput> *firstResponse = (id)self.nextResponder;

    UITextRange *range = firstResponse.selectedTextRange;
    if ([firstResponse respondsToSelector:@selector(shouldChangeTextInRange:replacementText:)] && [firstResponse shouldChangeTextInRange:range replacementText:string] == NO) {
        return;
    }
    [firstResponse replaceRange:range withText:string];
}

评论输入框

依据inputAccessoryView的特性,我们也可以将评论输入框 A 作为某中介输入框 B 的inputAccessoryView

当输入框 B 成为第一响应者时,由于评论输入框 A 是输入框 B 的inputAccessoryView,因此其位于键盘的头部。

监听键盘弹出事件,在键盘将要弹出时把第一响应者嫁接给评论输入框 B。

这时,评论输入框 A既是第一响应者,又是键盘的inputAccessoryView

inputAccessoryView

- (void)add_textField
{
    self.textField_keyword = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 0, 50)];
    self.textField_keyword.backgroundColor = [UIColor colorWithRed:170/255.0 green:170/255.0 blue:170/255.0 alpha:1];
    self.textField_keyword.leftView     = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 50)];
    self.textField_keyword.leftViewMode = UITextFieldViewModeAlways;
    self.textField_keyword.rightView    = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 50)];
    self.textField_keyword.rightViewMode= UITextFieldViewModeAlways;

    self.textField_temp = [[UITextField alloc]initWithFrame:CGRectZero];
    [self.view addSubview:self.textField_temp];
    self.textField_temp.inputAccessoryView = self.textField_keyword;
}
#pragma mark-textField event
- (void)keyboardWillShow
{
    [self.textField_keyword becomeFirstResponder];
}
- (IBAction)showTextInputView:(id)sender {
    [self.textField_temp becomeFirstResponder];
}

#pragma mark-life cycle
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow) name:UIKeyboardWillShowNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

自定义输入法

ptdiagitalkeyboard's People

Stargazers

iStud avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.