iOS开发之自定义键盘(数字,字母类型等随意切换)

时间:2024-05-19 15:01:03

项目开发很多时候用系统给的键盘不是很满足自身实际需求,那就自定义一个吧:

方法其实很简单,重新定义一个view,继承UItextfield,把UI设计好的需求键盘加入新的otherKeyboardView,然后执行代码:

 self.inputView =self.otherKeyBoardView;

  [selfreloadInputViews];


具体效果如下:iOS开发之自定义键盘(数字,字母类型等随意切换)iOS开发之自定义键盘(数字,字母类型等随意切换)


自己封装了一个PassTextField类,直接调用就ok:

- (void)viewDidLoad {

    [super viewDidLoad];

    

    //自定义键盘调用

    PassTextFild *searchTextField =[[PassTextFild alloc]initWithFrame:CGRectMake(20,50, 280, 28)];

    searchTextField.borderStyle =UITextBorderStyleRoundedRect;

    searchTextField.placeholder =@"请输入代码或拼音首字母";

    searchTextField.font =[UIFont systemFontOfSize:13];

    searchTextField.contentVerticalAlignment =UIControlContentVerticalAlignmentCenter;

    searchTextField.autocorrectionType =UITextAutocorrectionTypeNo;

    searchTextField.returnKeyType = UIReturnKeyDone;

    searchTextField.keyboardType =UIKeyboardTypeDefault;

    searchTextField.clearButtonMode = UITextFieldViewModeAlways;

    searchTextField.text =@"";

    [self.view addSubview:searchTextField];

    [searchTextField becomeFirstResponder];

}


主要功能代码如下:

if (pressButtonCapital.selected)

    {

        addString =[addString uppercaseString];

    }

    NSMutableString* mutableString = [[NSMutableStringalloc] initWithFormat:@"%@%@",self.text, addString];

    self.text = mutableString;

    [selfsearchAllTextField];


//切换到123键盘

-(void)changeNumberKeyBoard

{

    self.inputView =keyBoardView;

    [selfreloadInputViews];

}

//空格

-(void)addNullAction

{

    NSMutableString* mutableString = [[NSMutableStringalloc] initWithFormat:@"%@ ",self.text];

    self.text = mutableString;

    [selfsearchAllTextField];

}


//大小写切换

-(void)pre*italAction:(UIButton *)button

{

    if (button.selected){

        button.selected =NO;

        bacDaXieImage.image =[UIImageimageNamed:@"da_.png"];

    }else{

        button.selected =YES;

        bacDaXieImage.image =[UIImageimageNamed:@"da.png"];

    }

}



好吧,废话不多说了,具体dema直接下载:https://github.com/Deng0512/SelfKeyBoard点击打开链接