IOS 自定义数字键盘

Posted on 2014-06-23 17:57  iBaby  阅读(470)  评论(0)    收藏  举报

鉴于安全方面考虑,密码输入所使用的键盘不应该用系统键盘,当用户使用第三方输入法时,密码将会被第三方输入法截获,所以我们在有些安全性要求比较高的地方要自定义键盘(哪怕是和系统键盘长的一模一样)。

 

初始化键盘

- (id)initWithFrame:(CGRect)frame andArray:(NSArray *)numArray{

    self = [super initWithFrame:frame];

    if (self) {

        self.numArray = numArray;

        self.bounds = CGRectMake(0, 0, 320, 216);

        for (int i=0; i<4; i++)

        {

            for (int j=0; j<3; j++)

            {

                UIButton *button = [self creatButtonWithX:i Y:j];

                [self addSubview:button];

            }

        }

        UIColor *color = [UIColor colorWithRed:169.0/255 green:181.0/255 blue:192.0/255 alpha:1.0];

        UIView *line1 = [[[UIView alloc] initWithFrame:CGRectMake(105, 0, kMPLineWidth, 216)] autorelease];

        line1.backgroundColor = color;

        [self addSubview:line1];

        UIView *line2 = [[[UIView alloc] initWithFrame:CGRectMake(214, 0, kMPLineWidth, 216)] autorelease];

        line2.backgroundColor = color;

        [self addSubview:line2];

        for (int i=0; i<3; i++)

        {

            UIView *line = [[[UIView alloc] initWithFrame:CGRectMake(0, 54*(i+1), 320, kMPLineWidth)] autorelease];

            line.backgroundColor = color;

            [self addSubview:line];

        }

    }

    return self;

}

 

生成键盘按键

 

-(UIButton *)creatButtonWithX:(NSInteger) x Y:(NSInteger) y

{

    UIButton *button;

    CGFloat frameX;

    CGFloat frameW;

    switch (y)

    {

        case 0:

            frameX = 0.0;

            frameW = 106.0;

            break;

        case 1:

            frameX = 105.0;

            frameW = 110.0;

            break;

        case 2:

            frameX = 214.0;

            frameW = 106.0;

            break;

        default:

            break;

    }

    CGFloat frameY = 54*x;

    button = [[UIButton alloc] initWithFrame:CGRectMake(frameX, frameY, frameW, 54)];

    NSInteger num = y+3*x+1;

    if (num<10) {

        button.tag = [[self.numArray objectAtIndex:num-1] intValue];

    }

    else if(num == 11) {

        button.tag = [[self.numArray lastObject] intValue];

    }

    else {

        button.tag = num;

    }

    [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];

    

    UIColor *colorNormal = MP_RGB(253, 253, 253);

    UIColor *colorHightlighted = MP_RGB(187, 190, 195);

    if (num == 10 || num == 12)

    {

        colorNormal = MP_RGB(187, 190, 195);

        colorHightlighted = MP_RGB(253, 253, 253);

    }

    button.backgroundColor = colorNormal;

    CGSize imageSize = CGSizeMake(frameW, 54);

    UIGraphicsBeginImageContextWithOptions(imageSize, 0, [UIScreen mainScreen].scale);

    [colorHightlighted set];

    UIRectFill(CGRectMake(0, 0, imageSize.width, imageSize.height));

    UIImage *pressedColorImg = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    [button setImage:pressedColorImg forState:UIControlStateHighlighted];

 

    if (num<10)

    {

        UILabel *labelNum = [[[UILabel alloc] initWithFrame:CGRectMake(0, 15, frameW, 24)] autorelease];

        labelNum.text = [NSString stringWithFormat:@"%d",[[self.numArray objectAtIndex:num-1] intValue]];

        labelNum.textColor = [UIColor blackColor];

        labelNum.textAlignment = NSTextAlignmentCenter;

        labelNum.font = kMPNumFont;

        [button addSubview:labelNum];

    }

    else if (num == 11)

    {

        UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0, 15, frameW, 28)] autorelease];

        label.text = [NSString stringWithFormat:@"%d",[[self.numArray lastObject] intValue]];

        label.textColor = [UIColor blackColor];

        label.textAlignment = NSTextAlignmentCenter;

        label.font = kMPNumFont;

        [button addSubview:label];

    }

    else if (num == 10)

    {

        UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0, 15, frameW, 28)] autorelease];

        label.text = @"X";

        label.textColor = [UIColor blackColor];

        label.textAlignment = NSTextAlignmentCenter;

        [button addSubview:label];

    }

    else

    {

        UIImageView *arrow = [[[UIImageView alloc] initWithFrame:CGRectMake(42, 19, 22, 17)] autorelease];

        arrow.image = [UIImage imageNamed:@"arrowInKeyboard"];

        [button addSubview:arrow];

        

    }

    return [button autorelease];

}

 

键盘按键的点击响应

 

-(void)clickButton:(UIButton *)sender

{

    if (sender.tag == 10)

    {

        [self.delegate numberKeyboardXBtnTapped];

        return;

    }

    else if(sender.tag == 12)

    {

        [self.delegate numberKeyboardBackspace];

    }

    else

    {

        NSInteger num = sender.tag;

        if (sender.tag == 11)

        {

            num = 0;

        }

        [self.delegate numberKeyboardInput:num];

    }

}