自定义数字键盘

自定义数据键盘

* .h 文件

@protocol SLNumericKeyboardDelegate <NSObject>

- (void) numberKeyboardInput:(NSInteger) number;
- (void) numberKeyboardBackspaceTag:(NSInteger)tag;
- (void) changeKeyboardType;
@end

@interface SLNumericKeyboard : UIView {

    NSArray *arrLetter;
}
@property (nonatomic,assign) id<SLNumericKeyboardDelegate> delegate;
@property (nonatomic,retain) UILabel *customLbl;  //!< 自定义的一个键
@end

* .m 文件


#import "SLNumericKeyboard.h"

#define kLineWidth .5
#define kNumFont [UIFont systemFontOfSize:28]
#define SCREENWIDTH  [UIScreen mainScreen].bounds.size.width

@implementation SLNumericKeyboard

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];

    if (self) {

        self.bounds = CGRectMake(0, 0, SCREENWIDTH, 216);
        arrLetter = [NSArray arrayWithObjects:@"ABC",@"DEF",@"GHI",@"JKL",@"MNO",@"PQRS",@"TUV",@"WXYZ", nil];
        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 lightGrayColor];// [UIColor colorWithRed:188/255.0 green:192/255.0 blue:199/255.0 alpha:1];

//        UIView *line0 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, .6)];
//        line0.backgroundColor = color;
//        [self addSubview:line0];

        UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(SCREENWIDTH / 3, 0, kLineWidth, 216)];
        line1.backgroundColor = color;
        [self addSubview:line1];

        UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(SCREENWIDTH/3*2+1, 0, kLineWidth, 216)];
        line2.backgroundColor = color;
        [self addSubview:line2];

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

            UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 54*(i+1), SCREENWIDTH, kLineWidth)];
            line.backgroundColor = color;
            [self addSubview:line];
        }

    }
    return self;
}

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

    UIButton *button;
    button.backgroundColor = [UIColor redColor];

    CGFloat frameX;
    CGFloat frameW = SCREENWIDTH / 3;
    switch (y) {

        case 0:
            frameX = 0.0;
            break;
        case 1:
            frameX = frameW + 1;
            break;
        case 2:
            frameX = frameW * 2 + 2;
            break;

        default:
            break;
    }
    CGFloat frameY = 54*x;

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

    NSInteger num = y+3*x+1;
    button.tag = num;
    [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];

    UIColor *colorNormal = [UIColor whiteColor];// [UIColor colorWithRed:252/255.0 green:252/255.0 blue:252/255.0 alpha:1];
    UIColor *colorHightlighted = [UIColor colorWithRed:186.0/255 green:189.0/255 blue:194.0/255 alpha:1.0];

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

        UIColor *colorTemp = colorNormal;
        colorNormal = colorHightlighted;
        colorHightlighted = colorTemp;
    }

    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, 5, frameW, 28)];
        labelNum.text = [NSString stringWithFormat:@"%ld",num];
        labelNum.textColor = [UIColor blackColor];
        labelNum.textAlignment = NSTextAlignmentCenter;
        labelNum.backgroundColor = [UIColor clearColor];
        labelNum.font = kNumFont;
        [button addSubview:labelNum];

        if (num != 1) {

            UILabel *labelLetter = [[UILabel alloc] initWithFrame:CGRectMake(0, 33, frameW, 16)];
            labelLetter.text = [arrLetter objectAtIndex:num-2];
            labelLetter.textColor = [UIColor blackColor];
            labelLetter.textAlignment = NSTextAlignmentCenter;
            labelLetter.font = [UIFont systemFontOfSize:12];
            labelLetter.backgroundColor = [UIColor clearColor];
            [button addSubview:labelLetter];
        }
    } else if (num == 11) {

        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 15, frameW, 28)];
        label.text = @"0";
        label.textColor = [UIColor blackColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.backgroundColor = [UIColor clearColor];
        label.font = kNumFont;
        [button addSubview:label];

    } else if (num == 10) {

        _customLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 15, frameW, 28)];
        _customLbl.text = @"完成";
        _customLbl.backgroundColor = [UIColor clearColor];
        _customLbl.textColor = [UIColor blackColor];
        _customLbl.textAlignment = NSTextAlignmentCenter;
        [button addSubview:_customLbl];

    } else {

        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(btnLong:)];
        longPress.minimumPressDuration = 0.8; //定义按的时间
        [button addGestureRecognizer:longPress];

        UIImageView *arrow = [[UIImageView alloc] initWithFrame:CGRectMake((SCREENWIDTH/3 - 22) / 2, 19, 22, 17)];
        arrow.image = [UIImage imageNamed:@"arrowInKeyboard"];
        [button addSubview:arrow];
    }

    return button;
}


- (void)btnLong:(UILongPressGestureRecognizer *)longPress {

    [self.delegate numberKeyboardBackspaceTag:20];
}

-(void)clickButton:(UIButton *)sender {

    if (sender.tag == 10) {

        [self.delegate changeKeyboardType];
        return;
    } else if(sender.tag == 12) {

        [self.delegate numberKeyboardBackspaceTag:sender.tag];
    } else {

        NSInteger num = sender.tag;
        if (sender.tag == 11)
            num = 0;
        [self.delegate numberKeyboardInput:num];
    }
}

@end

里都很简单所以没加什么注释!
不明白的可以找我 QQ:2298527161

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值