iOS安全键盘

原文链接:https://blog.csdn.net/laodeng0512/article/details/75529630

实现:

//
//  PassTextFild.h
//  VisionField
//
//  Created by 邓 连喜 on 14-8-22.
//  Copyright (c) 2014年 邓 连喜. All rights reserved.
//

#import <UIKit/UIKit.h>

@protocol PassTextFieldViewDelegate <NSObject>
-(void)passTextFieldSearch:(NSString *)searchString;
@end

@interface PassTextFild : UITextField
{
    UIView *keyBoardView;
    UIButton *pressButtonCapital;
    UIImageView *bacDaXieImage;
}
@property(nonatomic,strong)UIView *otherKeyBoardView;
@property(nonatomic,weak)id<PassTextFieldViewDelegate>delegate;
@end

//
//  PassTextFild.m
//  VisionField
//
//  Created by 邓 连喜 on 14-8-22.
//  Copyright (c) 2014年 邓 连喜. All rights reserved.
//

#import "PassTextFild.h"

#define FIT_X          [UIScreen mainScreen].bounds.size.width/320.000
#define iPhone4        ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)
#define IS_iPhoneX ([UIScreen mainScreen].bounds.size.width >= 375 && [UIScreen mainScreen].bounds.size.height >= 812)

@implementation PassTextFild
@synthesize otherKeyBoardView=_otherKeyBoardView;


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self loadBegin];
    }
    return self;
}

-(void)loadBegin
{
    keyBoardView =[[UIView alloc]initWithFrame:CGRectMake(0, IS_iPhoneX ? 480-220 - 30 : 480-220, 320, 240)];
    if (iPhone4) {
        keyBoardView.frame =CGRectMake(0, 480-220, 320, 240);
    }else{
        keyBoardView.frame =CGRectMake(0, 568*FIT_X- (IS_iPhoneX ? 270 * FIT_X : 240 * FIT_X), 320*FIT_X, IS_iPhoneX ?  270*FIT_X : 240*FIT_X);
    }
    keyBoardView.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"bgs.png"]];
    
    for (int i=0; i<10; i++)
    {
        UIButton *pressButton =[UIButton buttonWithType:UIButtonTypeCustom];
        [pressButton setBackgroundImage:[UIImage imageNamed:@"anniu1.png"] forState:UIControlStateNormal];
        [pressButton setBackgroundImage:[UIImage imageNamed:@"anniu1_select.png"] forState:UIControlStateHighlighted];
        pressButton.tag =i+1;
        pressButton.frame =[self frameForSettingButtonUnderImage:i];
        [pressButton addTarget:self action:@selector(goPress:) forControlEvents:UIControlEventTouchUpInside];
        [keyBoardView addSubview:pressButton];
        
        UILabel *descripSecLabel =[[UILabel alloc] init];
        descripSecLabel.frame =pressButton.frame;
        descripSecLabel.backgroundColor =[UIColor clearColor];
        descripSecLabel.textAlignment =NSTextAlignmentCenter;
        descripSecLabel.font =[UIFont systemFontOfSize:18*FIT_X];
        descripSecLabel.text =[NSString stringWithFormat:@"%d",i+1];
        if (i==9) {
            descripSecLabel.text =@"0";
        }
        [keyBoardView addSubview:descripSecLabel];
        
    }
    //减
    UIButton *pressButtondelete =[UIButton buttonWithType:UIButtonTypeCustom];
    pressButtondelete.frame =CGRectMake(257*FIT_X, 5*FIT_X, 58*FIT_X, 55*FIT_X);
    [pressButtondelete setBackgroundImage:[UIImage imageNamed:@"anniu2.png"] forState:UIControlStateNormal];
    [pressButtondelete setBackgroundImage:[UIImage imageNamed:@"anniu2_select.png"] forState:UIControlStateHighlighted];
    [pressButtondelete addTarget:self action:@selector(deleteTextField) forControlEvents:UIControlEventTouchUpInside];
    [keyBoardView addSubview:pressButtondelete];
    
    UIImageView *backGrodImage =[[UIImageView alloc]initWithFrame:CGRectMake(270*FIT_X, 18*FIT_X, 32*FIT_X, 28*FIT_X)];
    backGrodImage.image =[UIImage imageNamed:@"tu2 (3).png"];
    [keyBoardView addSubview:backGrodImage];
    
    //清空
    UIButton *pressButtonClearAll =[UIButton buttonWithType:UIButtonTypeCustom];
    pressButtonClearAll.frame =CGRectMake(257*FIT_X, 65*FIT_X, 58*FIT_X, 55*FIT_X);
    [pressButtonClearAll setBackgroundImage:[UIImage imageNamed:@"anniu2.png"] forState:UIControlStateNormal];
    [pressButtonClearAll setBackgroundImage:[UIImage imageNamed:@"anniu2_select.png"] forState:UIControlStateHighlighted];
    [pressButtonClearAll addTarget:self action:@selector(clearAllTextField) forControlEvents:UIControlEventTouchUpInside];
    [keyBoardView addSubview:pressButtonClearAll];
    
    UILabel *pressButtonClearAllLabel =[[UILabel alloc] init];
    pressButtonClearAllLabel.frame =pressButtonClearAll.frame;
    pressButtonClearAllLabel.backgroundColor =[UIColor clearColor];
    pressButtonClearAllLabel.textColor =[UIColor whiteColor];
    pressButtonClearAllLabel.textAlignment =NSTextAlignmentCenter;
    pressButtonClearAllLabel.font =[UIFont systemFontOfSize:18*FIT_X];
    pressButtonClearAllLabel.text =@"清空";
    [keyBoardView addSubview:pressButtonClearAllLabel];
    
    //隐藏
    UIButton *pressButtonReturn =[UIButton buttonWithType:UIButtonTypeCustom];
    pressButtonReturn.frame =CGRectMake(257*FIT_X, 125*FIT_X, 58*FIT_X, 55*FIT_X);
    [pressButtonReturn setBackgroundImage:[UIImage imageNamed:@"anniu2.png"] forState:UIControlStateNormal];
    [pressButtonReturn setBackgroundImage:[UIImage imageNamed:@"anniu2_select.png"] forState:UIControlStateHighlighted];
    [pressButtonReturn addTarget:self action:@selector(goReturn) forControlEvents:UIControlEventTouchUpInside];
    [keyBoardView addSubview:pressButtonReturn];
    
    UILabel *pressButtonReturnLabel =[[UILabel alloc] init];
    pressButtonReturnLabel.frame =pressButtonReturn.frame;
    pressButtonReturnLabel.backgroundColor =[UIColor clearColor];
    pressButtonReturnLabel.textColor =[UIColor whiteColor];
    pressButtonReturnLabel.textAlignment =NSTextAlignmentCenter;
    pressButtonReturnLabel.font =[UIFont systemFontOfSize:18*FIT_X];
    pressButtonReturnLabel.text =@"隐藏";
    [keyBoardView addSubview:pressButtonReturnLabel];
    
    //确定
    UIButton *pressButtonSearch =[UIButton buttonWithType:UIButtonTypeCustom];
    pressButtonSearch.frame =CGRectMake(257*FIT_X, 185*FIT_X, 58*FIT_X, 55*FIT_X);
    [pressButtonSearch setBackgroundImage:[UIImage imageNamed:@"anniu2.png"] forState:UIControlStateNormal];
    [pressButtonSearch setBackgroundImage:[UIImage imageNamed:@"anniu2_select.png"] forState:UIControlStateHighlighted];
    [pressButtonSearch addTarget:self action:@selector(searchAllTextField) forControlEvents:UIControlEventTouchUpInside];
    [keyBoardView addSubview:pressButtonSearch];
    
    UILabel *pressButtonSearchLabel =[[UILabel alloc] init];
    pressButtonSearchLabel.frame =pressButtonSearch.frame;
    pressButtonSearchLabel.backgroundColor =[UIColor clearColor];
    pressButtonSearchLabel.textColor =[UIColor whiteColor];
    pressButtonSearchLabel.textAlignment =NSTextAlignmentCenter;
    pressButtonSearchLabel.font =[UIFont systemFontOfSize:18*FIT_X];
    pressButtonSearchLabel.text =@"确定";
    [keyBoardView addSubview:pressButtonSearchLabel];
    
    //空格
    UIButton *pressButtonZeroTwo =[UIButton buttonWithType:UIButtonTypeCustom];
    pressButtonZeroTwo.frame =CGRectMake(194*FIT_X, 185*FIT_X, 58*FIT_X, 55*FIT_X);
    [pressButtonZeroTwo setBackgroundImage:[UIImage imageNamed:@"anniu2.png"] forState:UIControlStateNormal];
    [pressButtonZeroTwo setBackgroundImage:[UIImage imageNamed:@"anniu2_select.png"] forState:UIControlStateHighlighted];
    pressButtonZeroTwo.tag =24;
    [pressButtonZeroTwo addTarget:self action:@selector(goPressOtherButtons:) forControlEvents:UIControlEventTouchUpInside];
    [keyBoardView addSubview:pressButtonZeroTwo];
    
    UILabel *pressButtonZeroTwoLabel =[[UILabel alloc] init];
    pressButtonZeroTwoLabel.frame =pressButtonZeroTwo.frame;
    pressButtonZeroTwoLabel.backgroundColor =[UIColor clearColor];
    pressButtonZeroTwoLabel.textColor =[UIColor whiteColor];
    pressButtonZeroTwoLabel.textAlignment =NSTextAlignmentCenter;
    pressButtonZeroTwoLabel.font =[UIFont systemFontOfSize:18*FIT_X];
    pressButtonZeroTwoLabel.text =@"空格";
    [keyBoardView addSubview:pressButtonZeroTwoLabel];
    
    //下划线
    UIButton *pressButtonThreeZero =[UIButton buttonWithType:UIButtonTypeCustom];
    pressButtonThreeZero.frame =CGRectMake(68*FIT_X, 185*FIT_X, 58*FIT_X, 55*FIT_X);
    [pressButtonThreeZero setBackgroundImage:[UIImage imageNamed:@"anniu2.png"] forState:UIControlStateNormal];
    [pressButtonThreeZero setBackgroundImage:[UIImage imageNamed:@"anniu2_select.png"] forState:UIControlStateHighlighted];
    pressButtonThreeZero.tag =23;
    [pressButtonThreeZero addTarget:self action:@selector(goPressOtherButtons:) forControlEvents:UIControlEventTouchUpInside];
    [keyBoardView addSubview:pressButtonThreeZero];
    
    UILabel *pressButtonThreeZeroLabel =[[UILabel alloc] init];
    pressButtonThreeZeroLabel.frame =pressButtonThreeZero.frame;
    pressButtonThreeZeroLabel.backgroundColor =[UIColor clearColor];
    pressButtonThreeZeroLabel.textColor =[UIColor whiteColor];
    pressButtonThreeZeroLabel.textAlignment =NSTextAlignmentCenter;
    pressButtonThreeZeroLabel.font =[UIFont systemFontOfSize:18*FIT_X];
    pressButtonThreeZeroLabel.text =@"_";
    [keyBoardView addSubview:pressButtonThreeZeroLabel];
    
    //ABC
    UIButton *pressButtonABC =[UIButton buttonWithType:UIButtonTypeCustom];
    pressButtonABC.frame =CGRectMake(5*FIT_X, 185*FIT_X, 58*FIT_X, 55*FIT_X);
    [pressButtonABC setBackgroundImage:[UIImage imageNamed:@"anniu2.png"] forState:UIControlStateNormal];
    [pressButtonABC setBackgroundImage:[UIImage imageNamed:@"anniu2_select.png"] forState:UIControlStateHighlighted];
    [pressButtonABC addTarget:self action:@selector(changeAbcKeyBoard) forControlEvents:UIControlEventTouchUpInside];
    [keyBoardView addSubview:pressButtonABC];
    
    UILabel *pressButtonABCLabel =[[UILabel alloc] init];
    pressButtonABCLabel.frame =pressButtonABC.frame;
    pressButtonABCLabel.backgroundColor =[UIColor clearColor];
    pressButtonABCLabel.textColor =[UIColor whiteColor];
    pressButtonABCLabel.textAlignment =NSTextAlignmentCenter;
    pressButtonABCLabel.font =[UIFont systemFontOfSize:18*FIT_X];
    pressButtonABCLabel.text =@"ABC";
    [keyBoardView addSubview:pressButtonABCLabel];
    
    //,
    UIButton *pressButtonSixZero=[UIButton buttonWithType:UIButtonTypeCustom];
    pressButtonSixZero.frame =CGRectMake(5*FIT_X, 5*FIT_X, 58*FIT_X, 55*FIT_X);
    [pressButtonSixZero setBackgroundImage:[UIImage imageNamed:@"anniu2.png"] forState:UIControlStateNormal];
    [pressButtonSixZero setBackgroundImage:[UIImage imageNamed:@"anniu2_select.png"] forState:UIControlStateHighlighted];
    pressButtonSixZero.tag =20;
    [pressButtonSixZero addTarget:self action:@selector(goPressOtherButtons:) forControlEvents:UIControlEventTouchUpInside];
    [keyBoardView addSubview:pressButtonSixZero];
    
    UILabel *pressButtonSixZeroLabel =[[UILabel alloc] init];
    pressButtonSixZeroLabel.frame =pressButtonSixZero.frame;
    pressButtonSixZeroLabel.backgroundColor =[UIColor clearColor];
    pressButtonSixZeroLabel.textColor =[UIColor whiteColor];
    pressButtonSixZeroLabel.textAlignment =NSTextAlignmentCenter;
    pressButtonSixZeroLabel.font =[UIFont systemFontOfSize:18*FIT_X];
    pressButtonSixZeroLabel.text =@",";
    [keyBoardView addSubview:pressButtonSixZeroLabel];
    
    //.
    UIButton *pressButtonSixOne =[UIButton buttonWithType:UIButtonTypeCustom];
    pressButtonSixOne.frame =CGRectMake(5*FIT_X, 65*FIT_X, 58*FIT_X, 55*FIT_X);
    [pressButtonSixOne setBackgroundImage:[UIImage imageNamed:@"anniu2.png"] forState:UIControlStateNormal];
    [pressButtonSixOne setBackgroundImage:[UIImage imageNamed:@"anniu2_select.png"] forState:UIControlStateHighlighted];
    pressButtonSixOne.tag =21;
    [pressButtonSixOne addTarget:self action:@selector(goPressOtherButtons:) forControlEvents:UIControlEventTouchUpInside];
    [keyBoardView addSubview:pressButtonSixOne];
    
    UILabel *pressButtonSixOneLabel =[[UILabel alloc] init];
    pressButtonSixOneLabel.frame =pressButtonSixOne.frame;
    pressButtonSixOneLabel.backgroundColor =[UIColor clearColor];
    pressButtonSixOneLabel.textColor =[UIColor whiteColor];
    pressButtonSixOneLabel.textAlignment =NSTextAlignmentCenter;
    pressButtonSixOneLabel.font =[UIFont systemFontOfSize:18*FIT_X];
    pressButtonSixOneLabel.text =@".";
    [keyBoardView addSubview:pressButtonSixOneLabel];
    
    //:
    UIButton *pressButtonZeroZero =[UIButton buttonWithType:UIButtonTypeCustom];
    pressButtonZeroZero.frame =CGRectMake(5*FIT_X, 125*FIT_X, 58*FIT_X, 55*FIT_X);
    [pressButtonZeroZero setBackgroundImage:[UIImage imageNamed:@"anniu2.png"] forState:UIControlStateNormal];
    [pressButtonZeroZero setBackgroundImage:[UIImage imageNamed:@"anniu2_select.png"] forState:UIControlStateHighlighted];
    pressButtonZeroZero.tag =22;
    [pressButtonZeroZero addTarget:self action:@selector(goPressOtherButtons:) forControlEvents:UIControlEventTouchUpInside];
    [keyBoardView addSubview:pressButtonZeroZero];
    
    UILabel *pressButtonZeroZeroLabel =[[UILabel alloc] init];
    pressButtonZeroZeroLabel.frame =pressButtonZeroZero.frame;
    pressButtonZeroZeroLabel.backgroundColor =[UIColor clearColor];
    pressButtonZeroZeroLabel.textColor =[UIColor whiteColor];
    pressButtonZeroZeroLabel.textAlignment =NSTextAlignmentCenter;
    pressButtonZeroZeroLabel.font =[UIFont systemFontOfSize:18*FIT_X];
    pressButtonZeroZeroLabel.text =@":";
    [keyBoardView addSubview:pressButtonZeroZeroLabel];
    
    self.inputView =keyBoardView;
}

-(void)goPress:(UIButton *)btn
{
    NSInteger number = btn.tag;
    if (number==10) {
        number =0;
    }
    
    NSMutableString* mutableString = [[NSMutableString alloc] initWithFormat:@"%@%ld", self.text, number];
    self.text = mutableString;
    
    [self searchAllTextField];
}

-(void)goPressOtherButtons:(UIButton *)btn
{
    
    NSString *addString=@"";
    if (btn.tag==20) {
        addString =@",";
    }
    else if (btn.tag==21)
    {
        addString =@".";
    }
    else if (btn.tag==22)
    {
        addString =@":";
    }
    else if (btn.tag==23)
    {
        addString =@"_";
    }
    else if (btn.tag==24)
    {
        addString =@" ";
    }
    
    NSMutableString* mutableString = [[NSMutableString alloc] initWithFormat:@"%@%@", self.text, addString];
    self.text = mutableString;
    
    [self searchAllTextField];
}


//删除一位
-(void)deleteTextField
{
    NSMutableString* mutableString = [[NSMutableString alloc] initWithFormat:@"%@", self.text];
    if ([mutableString length] > 0) {
        NSRange tmpRange;
        tmpRange.location = [mutableString length] - 1;
        tmpRange.length = 1;
        [mutableString deleteCharactersInRange:tmpRange];
    }
    self.text = mutableString;
    
    [self searchAllTextField];
}

//清除
-(void)clearAllTextField
{
    self.text =@"";
}

//确定
-(void)searchAllTextField
{
    if ([self.text length]>0)
    {
        if (self.delegate) {
            [self.delegate passTextFieldSearch:self.text];
        }
    }
//    else
//    {
        [self resignFirstResponder];
//        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"请先输入需要确定的内容"
//                                                       message:@""
//                                                      delegate:nil
//                                             cancelButtonTitle:@"知道了"
//                                             otherButtonTitles:nil];
//        [alert show];
//        [self performSelector:@selector(removeAlertView:) withObject:alert afterDelay:1.5f];
//    }
}

//alertView 自动消失;
//- (void)removeAlertView:(UIAlertView *)alertView
//{
//    [alertView dismissWithClickedButtonIndex:[alertView cancelButtonIndex] animated:YES];
//    [alertView removeFromSuperview];
//}

//隐藏
-(void)goReturn
{
    [self resignFirstResponder];
}

-(CGRect)frameForSettingButtonUnderImage:(NSInteger)index
{
    CGFloat x;
    CGFloat y;
    int indexHeight;
    int height;
    indexHeight =60*FIT_X;
    height =5*FIT_X;
    
    if(index%3==0)
    {
        x =68*FIT_X;
        y =height +index/3 *indexHeight;
    }
    else if (index%3==1)
    {
        x =131*FIT_X;
        y =height +index/3 *indexHeight;
    }
    else
    {
        x =194*FIT_X;
        y =height +index/3*indexHeight;
    }
    
    if (index==9)
    {
        return CGRectMake(131*FIT_X, 185*FIT_X, 58*FIT_X, 55*FIT_X);
    }
    return CGRectMake(x,y,58*FIT_X,55*FIT_X);
}

//切换到abc键盘
-(void)changeAbcKeyBoard
{
    if (!self.otherKeyBoardView) {
        self.otherKeyBoardView =[[UIView alloc]initWithFrame:CGRectMake(0, 568-240, 320, 220)];
        if (iPhone4) {
            self.otherKeyBoardView.frame =CGRectMake(0, 480-220, 320, 220);
        }else{
            self.otherKeyBoardView.frame =CGRectMake(0, 568*FIT_X- (IS_iPhoneX ? 270 * FIT_X : 240 * FIT_X), 320*FIT_X, (IS_iPhoneX ? 250 * FIT_X : 220*FIT_X));
        }
        self.otherKeyBoardView.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"bgs.png"]];
        
        //第一排
        for (int i=0; i<10; i++)
        {
            UIButton *pressButton =[UIButton buttonWithType:UIButtonTypeCustom];
            [pressButton setBackgroundImage:[UIImage imageNamed:@"tu1 (2).png"] forState:UIControlStateNormal];
            [pressButton setBackgroundImage:[UIImage imageNamed:@"tu1 2.png"] forState:UIControlStateHighlighted];
            pressButton.tag =i+100;
            pressButton.frame =CGRectMake(1+i*32*FIT_X, 5*FIT_X, 34*FIT_X, 48*FIT_X);
            [pressButton addTarget:self action:@selector(goPressSecondKeyButtons:) forControlEvents:UIControlEventTouchUpInside];
            [self.otherKeyBoardView addSubview:pressButton];
            
            UILabel *descripSecLabel =[[UILabel alloc] init];
            descripSecLabel.frame =pressButton.frame;
            descripSecLabel.backgroundColor =[UIColor clearColor];
            descripSecLabel.textAlignment =NSTextAlignmentCenter;
            descripSecLabel.font =[UIFont systemFontOfSize:18*FIT_X];
            switch (i)
            {
                case 0:
                    descripSecLabel.text =@"Q";
                    break;
                case 1:
                    descripSecLabel.text =@"W";
                    break;
                case 2:
                    descripSecLabel.text =@"E";
                    break;
                case 3:
                    descripSecLabel.text =@"R";
                    break;
                case 4:
                    descripSecLabel.text =@"T";
                    break;
                case 5:
                    descripSecLabel.text =@"Y";
                    break;
                case 6:
                    descripSecLabel.text =@"U";
                    break;
                case 7:
                    descripSecLabel.text =@"I";
                    break;
                case 8:
                    descripSecLabel.text =@"O";
                    break;
                case 9:
                    descripSecLabel.text =@"P";
                    break;
                default:
                    break;
            }
            [self.otherKeyBoardView addSubview:descripSecLabel];
            
        }
        
        //第二排
        for (int i=0; i<9; i++)
        {
            UIButton *pressButton =[UIButton buttonWithType:UIButtonTypeCustom];
            [pressButton setBackgroundImage:[UIImage imageNamed:@"tu1 (2).png"] forState:UIControlStateNormal];
            [pressButton setBackgroundImage:[UIImage imageNamed:@"tu1 2.png"] forState:UIControlStateHighlighted];
            pressButton.tag =i+110;
            pressButton.frame =CGRectMake(16*FIT_X+i*32*FIT_X, 59*FIT_X, 34*FIT_X, 48*FIT_X);
            [pressButton addTarget:self action:@selector(goPressSecondKeyButtons:) forControlEvents:UIControlEventTouchUpInside];
            [self.otherKeyBoardView addSubview:pressButton];
            
            UILabel *descripSecLabel =[[UILabel alloc] init];
            descripSecLabel.frame =pressButton.frame;
            descripSecLabel.backgroundColor =[UIColor clearColor];
            descripSecLabel.textAlignment =NSTextAlignmentCenter;
            descripSecLabel.font =[UIFont systemFontOfSize:18*FIT_X];
            switch (i)
            {
                case 0:
                    descripSecLabel.text =@"A";
                    break;
                case 1:
                    descripSecLabel.text =@"S";
                    break;
                case 2:
                    descripSecLabel.text =@"D";
                    break;
                case 3:
                    descripSecLabel.text =@"F";
                    break;
                case 4:
                    descripSecLabel.text =@"G";
                    break;
                case 5:
                    descripSecLabel.text =@"H";
                    break;
                case 6:
                    descripSecLabel.text =@"J";
                    break;
                case 7:
                    descripSecLabel.text =@"K";
                    break;
                case 8:
                    descripSecLabel.text =@"L";
                    break;
                default:
                    break;
            }
            [self.otherKeyBoardView addSubview:descripSecLabel];
            
        }
        
        //第三排
        for (int i=0; i<7; i++)
        {
            UIButton *pressButton =[UIButton buttonWithType:UIButtonTypeCustom];
            [pressButton setBackgroundImage:[UIImage imageNamed:@"tu1 (2).png"] forState:UIControlStateNormal];
            [pressButton setBackgroundImage:[UIImage imageNamed:@"tu1 2.png"] forState:UIControlStateHighlighted];
            pressButton.tag =i+120;
            pressButton.frame =CGRectMake(46*FIT_X+i*32*FIT_X, 113*FIT_X, 34*FIT_X, 48*FIT_X);
            [pressButton addTarget:self action:@selector(goPressSecondKeyButtons:) forControlEvents:UIControlEventTouchUpInside];
            [self.otherKeyBoardView addSubview:pressButton];
            
            UILabel *descripSecLabel =[[UILabel alloc] init];
            descripSecLabel.frame =pressButton.frame;
            descripSecLabel.backgroundColor =[UIColor clearColor];
            descripSecLabel.textAlignment =NSTextAlignmentCenter;
            descripSecLabel.font =[UIFont systemFontOfSize:18*FIT_X];
            switch (i)
            {
                case 0:
                    descripSecLabel.text =@"Z";
                    break;
                case 1:
                    descripSecLabel.text =@"X";
                    break;
                case 2:
                    descripSecLabel.text =@"C";
                    break;
                case 3:
                    descripSecLabel.text =@"V";
                    break;
                case 4:
                    descripSecLabel.text =@"B";
                    break;
                case 5:
                    descripSecLabel.text =@"N";
                    break;
                case 6:
                    descripSecLabel.text =@"M";
                    break;
                default:
                    break;
            }
            [self.otherKeyBoardView addSubview:descripSecLabel];
        }
        
        //减
        UIButton *pressButtondelete =[UIButton buttonWithType:UIButtonTypeCustom];
        pressButtondelete.frame =CGRectMake(272*FIT_X, 113*FIT_X, 43*FIT_X, 49*FIT_X);
        [pressButtondelete setBackgroundImage:[UIImage imageNamed:@"tu2 2.png"] forState:UIControlStateNormal];
        [pressButtondelete setBackgroundImage:[UIImage imageNamed:@"tu2 (2).png"] forState:UIControlStateHighlighted];
        [pressButtondelete addTarget:self action:@selector(deleteTextField) forControlEvents:UIControlEventTouchUpInside];
        [self.otherKeyBoardView addSubview:pressButtondelete];
        
        UIImageView *backGrodImage =[[UIImageView alloc]initWithFrame:CGRectMake(277*FIT_X, 123*FIT_X, 32*FIT_X, 28*FIT_X)];
        backGrodImage.image =[UIImage imageNamed:@"tu2 (3).png"];
        [self.otherKeyBoardView addSubview:backGrodImage];
        
        //大写
        pressButtonCapital =[UIButton buttonWithType:UIButtonTypeCustom];
        pressButtonCapital.frame =CGRectMake(1, 113*FIT_X, 43*FIT_X, 49*FIT_X);
        [pressButtonCapital setBackgroundImage:[UIImage imageNamed:@"tu2 2.png"] forState:UIControlStateNormal];
        [pressButtonCapital setBackgroundImage:[UIImage imageNamed:@"tu2 (2).png"] forState:UIControlStateHighlighted];
        [pressButtonCapital setBackgroundImage:[UIImage imageNamed:@"tu2 (2).png"] forState:UIControlStateSelected];
        [pressButtonCapital addTarget:self action:@selector(pressCapitalAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.otherKeyBoardView addSubview:pressButtonCapital];
        
        bacDaXieImage =[[UIImageView alloc]initWithFrame:CGRectMake(7*FIT_X, 123*FIT_X, 31*FIT_X, 27*FIT_X)];
        bacDaXieImage.image =[UIImage imageNamed:@"da_.png"];
        [self.otherKeyBoardView addSubview:bacDaXieImage];
        
        //123
        UIButton *pressButtonReturn =[UIButton buttonWithType:UIButtonTypeCustom];
        pressButtonReturn.frame =CGRectMake(0, 167*FIT_X, 80*FIT_X, 49*FIT_X);
        [pressButtonReturn setBackgroundImage:[UIImage imageNamed:@"tu3-4"] forState:UIControlStateNormal];
        [pressButtonReturn setBackgroundImage:[UIImage imageNamed:@"tu3-04 (2).png"] forState:UIControlStateHighlighted];
        [pressButtonReturn addTarget:self action:@selector(changeNumberKeyBoard) forControlEvents:UIControlEventTouchUpInside];
        [self.otherKeyBoardView addSubview:pressButtonReturn];
        
        UILabel *pressButtonReturnLabel =[[UILabel alloc] init];
        pressButtonReturnLabel.frame =pressButtonReturn.frame;
        pressButtonReturnLabel.backgroundColor =[UIColor clearColor];
        pressButtonReturnLabel.textColor =[UIColor whiteColor];
        pressButtonReturnLabel.textAlignment =NSTextAlignmentCenter;
        pressButtonReturnLabel.font =[UIFont systemFontOfSize:18*FIT_X];
        pressButtonReturnLabel.text =@"1 2 3";
        [self.otherKeyBoardView addSubview:pressButtonReturnLabel];
        
        //空格
        UIButton *pressButtonZeroTwo =[UIButton buttonWithType:UIButtonTypeCustom];
        pressButtonZeroTwo.frame =CGRectMake(80*FIT_X, 167*FIT_X, 160*FIT_X, 49*FIT_X);
        [pressButtonZeroTwo setBackgroundImage:[UIImage imageNamed:@"tu4 (2).png"] forState:UIControlStateNormal];
        [pressButtonZeroTwo setBackgroundImage:[UIImage imageNamed:@"tu4 2.png"] forState:UIControlStateHighlighted];
        pressButtonZeroTwo.tag =24;
        [pressButtonZeroTwo addTarget:self action:@selector(addNullAction) forControlEvents:UIControlEventTouchUpInside];
        [self.otherKeyBoardView addSubview:pressButtonZeroTwo];
        
        UILabel *pressButtonZeroTwoLabel =[[UILabel alloc] init];
        pressButtonZeroTwoLabel.frame =pressButtonZeroTwo.frame;
        pressButtonZeroTwoLabel.backgroundColor =[UIColor clearColor];
        pressButtonZeroTwoLabel.textAlignment =NSTextAlignmentCenter;
        pressButtonZeroTwoLabel.font =[UIFont systemFontOfSize:18*FIT_X];
        pressButtonZeroTwoLabel.text =@"空格";
        [self.otherKeyBoardView addSubview:pressButtonZeroTwoLabel];
        
        //确定
        UIButton *pressButtonSearch =[UIButton buttonWithType:UIButtonTypeCustom];
        pressButtonSearch.frame =CGRectMake(240*FIT_X, 167*FIT_X, 80*FIT_X, 49*FIT_X);
        [pressButtonSearch setBackgroundImage:[UIImage imageNamed:@"tu3-4"] forState:UIControlStateNormal];
        [pressButtonSearch setBackgroundImage:[UIImage imageNamed:@"tu3-04 (2).png"] forState:UIControlStateHighlighted];
        [pressButtonSearch addTarget:self action:@selector(searchAllTextField) forControlEvents:UIControlEventTouchUpInside];
        [self.otherKeyBoardView addSubview:pressButtonSearch];
        
        UILabel *pressButtonSearchLabel =[[UILabel alloc] init];
        pressButtonSearchLabel.frame =pressButtonSearch.frame;
        pressButtonSearchLabel.backgroundColor =[UIColor clearColor];
        pressButtonSearchLabel.textColor =[UIColor whiteColor];
        pressButtonSearchLabel.textAlignment =NSTextAlignmentCenter;
//        pressButtonSearchLabel.font =[UIFont fontWithName:@"STHeitiSC-Light" size:18*FIT_X];
        pressButtonSearchLabel.font = [UIFont systemFontOfSize:18*FIT_X];
        pressButtonSearchLabel.text =@"确定";
        [self.otherKeyBoardView addSubview:pressButtonSearchLabel];
    }
    self.inputView =self.otherKeyBoardView;
    [self reloadInputViews];
}

-(void)goPressSecondKeyButtons:(UIButton *)btn
{
    
    NSString *addString=@"";
    switch (btn.tag)
    {
        case 100:
            addString =@"q";
            break;
        case 101:
            addString =@"w";
            break;
        case 102:
            addString =@"e";
            break;
        case 103:
            addString =@"r";
            break;
        case 104:
            addString =@"t";
            break;
        case 105:
            addString =@"y";
            break;
        case 106:
            addString =@"u";
            break;
        case 107:
            addString =@"i";
            break;
        case 108:
            addString =@"o";
            break;
        case 109:
            addString =@"p";
            break;
        case 110:
            addString =@"a";
            break;
        case 111:
            addString =@"s";
            break;
        case 112:
            addString =@"d";
            break;
        case 113:
            addString =@"f";
            break;
        case 114:
            addString =@"g";
            break;
        case 115:
            addString =@"h";
            break;
        case 116:
            addString =@"j";
            break;
        case 117:
            addString =@"k";
            break;
        case 118:
            addString =@"l";
            break;
        case 120:
            addString =@"z";
            break;
        case 121:
            addString =@"x";
            break;
        case 122:
            addString =@"c";
            break;
        case 123:
            addString =@"v";
            break;
        case 124:
            addString =@"b";
            break;
        case 125:
            addString =@"n";
            break;
        case 126:
            addString =@"m";
            break;
        default:
            break;
    }
    
    if (pressButtonCapital.selected)
    {
        addString =[addString uppercaseString];
    }
    NSMutableString* mutableString = [[NSMutableString alloc] initWithFormat:@"%@%@", self.text, addString];
    self.text = mutableString;
    [self searchAllTextField];
}

//切换到123键盘
-(void)changeNumberKeyBoard
{
    self.inputView =keyBoardView;
    [self reloadInputViews];
}

//空格
-(void)addNullAction
{
    NSMutableString* mutableString = [[NSMutableString alloc] initWithFormat:@"%@ ", self.text];
    self.text = mutableString;
    
    [self searchAllTextField];
}

//大小写切换
-(void)pressCapitalAction:(UIButton *)button
{
    if (button.selected)
    {
        button.selected =NO;
        bacDaXieImage.image =[UIImage imageNamed:@"da_.png"];
    }
    else
    {
        button.selected =YES;
        bacDaXieImage.image =[UIImage imageNamed:@"da.png"];
    }
}
@end

调用:


#import "ViewController.h"
#import "PassTextFild.h"

@interface ViewController ()<PassTextFieldViewDelegate>

@end

@implementation ViewController

- (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 =@"";
    searchTextField.delegate = self;
    [self.view addSubview:searchTextField];
    
    PassTextFild *searchTextField2 =[[PassTextFild alloc]initWithFrame:CGRectMake(20,150, 280, 28)];
    searchTextField2.borderStyle =UITextBorderStyleRoundedRect;
    searchTextField2.placeholder =@"请输入代码或拼音首字母";
    searchTextField2.font =[UIFont systemFontOfSize:13];
    searchTextField2.contentVerticalAlignment =UIControlContentVerticalAlignmentCenter;
    searchTextField2.autocorrectionType =UITextAutocorrectionTypeNo;
    searchTextField2.returnKeyType = UIReturnKeyDone;
    searchTextField2.keyboardType =UIKeyboardTypeDefault;
    searchTextField2.clearButtonMode = UITextFieldViewModeAlways;
    searchTextField2.text =@"";
    [self.view addSubview:searchTextField2];
    
    [searchTextField becomeFirstResponder];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)passTextFieldSearch:(NSString *)searchString {
    NSLog(@"%@",searchString);
}

@end

demo链接:
https://pan.baidu.com/s/1O2iiaERHpLuRhmPckTWpvA

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,098评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,213评论 2 380
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,960评论 0 336
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,519评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,512评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,533评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,914评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,574评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,804评论 1 296
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,563评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,644评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,350评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,933评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,908评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,146评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,847评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,361评论 2 342

推荐阅读更多精彩内容