自定義車牌輸入鍵盤

因?yàn)樽罱_發(fā)一款和停車有關(guān)的APP,在錄入車輛信息的時(shí)候需要輸入車牌信息,而車牌信息有很多字都是不常用的,輸入的時(shí)候很影響用戶輸入速度和體驗(yàn),在鍵字如飛的大佬面前簡(jiǎn)直不能忍,這時(shí)自定義一款鍵盤 專門用來(lái)輸入車牌,這就很舒服了。
自定義鍵盤也能增加安全系數(shù),雖然我這沒有涉及到。 先上效果圖:

2017年4月24日.png

然后是大概實(shí)現(xiàn)思路:
首先確定每個(gè)按鍵的坐標(biāo),將按鍵圖片和文字繪制到視圖上,再在touchesBegan 中確定點(diǎn)擊的按鈕,最后使用bolck回調(diào) 執(zhí)行響應(yīng)。
一步一步來(lái)實(shí)現(xiàn):
首先需要一個(gè)所有按鍵文字的數(shù)組。
1、確定按鍵坐標(biāo):
如果按鍵是有規(guī)律,直接用循環(huán)加上去就行,沒有規(guī)律那就手動(dòng)算吧。像效果圖1中就是直接用一個(gè)循環(huán)加上去的,效果圖2需要分列來(lái)添加。
2、繪制:

    CGRect keyBoardRect = CGRectMake(rect.origin.x + 2, rect.origin.y + 2, rect.size.width - 4, rect.size.height - 4);
    UIImage *image = [UIImage imageNamed:@"keyboard02"];
    [image drawInRect:keyBoardRect];
    CGSize imageSize = CGSizeMake(rect.size.width, rect.size.height);
    UIGraphicsBeginImageContext(imageSize);
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
    style.alignment = NSTextAlignmentCenter;
    UIFont *font = [UIFont systemFontOfSize:16];;
    CGFloat itemWidth = KSWidth/KHorizontalCount;;
    NSDictionary *attributes = @{NSFontAttributeName : font,
                                 NSParagraphStyleAttributeName : style,
                                 NSForegroundColorAttributeName : [UIColor blackColor]
                                 };
    CGFloat itemHeight = 20;
    CGRect textFrame = CGRectMake((rect.size.width - itemWidth)/2, (rect.size.height - itemHeight)/2, itemWidth, itemHeight);
    [text drawInRect:textFrame withAttributes:attributes];
    
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    [newImage drawInRect:keyBoardRect];

現(xiàn)將按鍵圖片繪制,然后將按鍵的文字繪制到圖片上。上面是主要代碼 ,最后會(huì)有demo。
3、確定按鍵點(diǎn)擊:
使用touchesBegan確定點(diǎn)擊的按鍵。
在touchesBegan之前,需要使用一個(gè)數(shù)組存儲(chǔ)每個(gè)按鍵的位置CGRect,在繪制按鍵時(shí)將按鍵的frame 轉(zhuǎn)換成NSValue 存在數(shù)組中。也就是按鍵上文字的數(shù)組和按鍵的位置frame是相對(duì)應(yīng)的。根據(jù)點(diǎn)擊的位置確定點(diǎn)擊的按鍵,這樣就很舒服了。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:self];
    
    [_drawRects enumerateObjectsUsingBlock:^(NSValue *rectValue, NSUInteger idx, BOOL * _Nonnull stop) {
        if (CGRectContainsPoint(rectValue.CGRectValue, location)) {
            NSString *clickKey = _data[idx];
            // 回調(diào)
            _clickKeyBoard(clickKey);
        }
    }];
}

4、獲取點(diǎn)擊回調(diào)
回調(diào)使用block或者協(xié)議什么都可以。我這里用block:
使用typedef定義一個(gè)bolck:

typedef void(^ClickKeyBoard)(NSString *character);

初始化:

- (instancetype)initWithFrame:(CGRect)frame withClickKeyBoard:(ClickKeyBoard)clickKeyBoard withDelete:(ClickDelete)clickDelete;

然后在獲取按鍵點(diǎn)擊時(shí)回調(diào)。
代碼不多也不復(fù)雜但也是個(gè)小功能,最后奉上<a >Demo</a>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容