深入講解iOS鍵盤三:自定義鍵盤的兩種方法

iOS系統(tǒng)提供了多種鍵盤,我們可以通過Enum類型設(shè)置。但有的時候由于某些特殊業(yè)務(wù)的需要,我們不得不自定義鍵盤,比如某些銀行的APP處于安全考慮,他們鍵盤數(shù)字的位置是隨機的,這個時候只能自定義鍵盤。幸運的是,iOS也為我們提供了多種方式自定義鍵盤。我們可以根據(jù)自身情況選擇合適的方案。

typedef?NS_ENUM(NSInteger,?UIKeyboardType)?{

????UIKeyboardTypeDefault,

????UIKeyboardTypeASCIICapable,?can?enter?ASCII?characters

????UIKeyboardTypeNumbersAndPunctuation,

????UIKeyboardTypeURL,

????UIKeyboardTypeNumberPad,

????UIKeyboardTypePhonePad,

????UIKeyboardTypeNamePhonePad,

????UIKeyboardTypeEmailAddress,

????UIKeyboardTypeDecimalPad?,

????UIKeyboardTypeTwitter?,

????UIKeyboardTypeWebSearch?,

????UIKeyboardTypeASCIICapableNumberPad?,

????UIKeyboardTypeAlphabet?=?UIKeyboardTypeASCIICapable,

};

對現(xiàn)有鍵盤稍加改動

這種情況適合身份證的輸入。由于身份證大部分情況下都是數(shù)字,偶爾可能出現(xiàn)“X”字符,如果我們棄用數(shù)字鍵盤直接使用諸如UIKeyboardTypeNumbersAndPunctuation等包含數(shù)字和字符的鍵盤又顯得沒有太大必要,那稍加改動數(shù)字鍵盤是個不錯的選擇。思路也很簡單,在彈出鍵盤的同時獲取鍵盤對應(yīng)的window,在window上加上我們需要的按鈕即可。如下是筆者改動的數(shù)字鍵盤。

下面,我對重點代碼做個講解:

-(void)keyboardWillShow:(NSNotification?*)notification{

????//移除掉原先添加的按鈕

????[self.extrakeyButton?removeFromSuperview];

????self.extrakeyButton?????=?nil;

????//這幾行代碼相信看了之前系列博客的讀者應(yīng)該很熟悉了

????NSDictionary?*userInfo??=?[notification?userInfo];

????CGFloat?animationDuration???=?[[userInfo?objectForKey:UIKeyboardAnimationDurationUserInfoKey]?floatValue];

????CGRect?kbEndFrame???????????=?[userInfo[UIKeyboardFrameEndUserInfoKey]?CGRectValue];

????CGFloat?kbHeight????????????=?kbEndFrame.size.height;

????//以下是對添加的X按鈕Frame的設(shè)置

????CGFloat?extrakeyButtonX?=?0;

????CGFloat?extrakeyButtonW?=?0;

????CGFloat?extrakeyButtonH?=?0;

????extrakeyButtonW?=?(SCREEN_WIDTH?-?7)?/?3;

????extrakeyButtonH?=?kbHeight?/?4;

????CGFloat?extrakeyButtonY?=?0;

????extrakeyButtonY?=?SCREEN_HEIGHT?+?kbHeight?-?extrakeyButtonH;

????//創(chuàng)建“X”按鈕,并設(shè)置相應(yīng)的屬性

????self.extrakeyButton?=?[[UIButton?alloc]?initWithFrame:CGRectMake(extrakeyButtonX,?extrakeyButtonY,?extrakeyButtonW,?extrakeyButtonH)];

????[self.extrakeyButton?addTarget:self?action:@selector(buttonDidClicked)?forControlEvents:UIControlEventTouchUpInside];

????self.extrakeyButton.titleLabel.font?=?[UIFont?systemFontOfSize:27];

????[self.extrakeyButton?setTitle:@"X"?forState:(UIControlStateNormal)];

????[self.extrakeyButton?setTitleColor:[UIColor?blackColor]?forState:UIControlStateNormal];

????//獲取鍵盤對應(yīng)的Window(這段代碼只在iOS?11上做過測試,還不夠嚴(yán)謹(jǐn))

????UIWindow?*tempWindow?=?[[[UIApplication?sharedApplication]?windows]?lastObject];

????[tempWindow?addSubview:self.extrakeyButton];

????//設(shè)置動畫

????[UIView?animateWithDuration:animationDuration?animations:^{

????????CGRect?frame????=?self.extrakeyButton.frame;

????????frame.origin.y??=?frame.origin.y?-?kbHeight;

????????self.extrakeyButton.frame?=?frame;

????}?completion:nil];

}

以上代碼只在iOS11的iPhone 8 Plus 上做過測試,可能不具備一定的普遍性,比如iPhone X的鍵盤位置有一定改變,按鈕的位置需要加代碼兼容。因此這種解決方案非常局限,只能用于身份證等業(yè)務(wù)非常簡單的備選方案。有個iOS開發(fā)者對這個鍵盤做了個封裝,讀者可以點擊這里獲取

自己設(shè)計一個鍵盤

iOS中可以通過設(shè)置TextField/TextView的inputView來定制鍵盤,

//The?custom?input?view?to?display?when?the?text?field?becomes?the?first?responder.

@property(readwrite,?strong)?UIView?*inputView;

這個自定義鍵盤就解決了我們開頭提到的問題:鍵盤上的數(shù)字是隨機排列的0-9,如果需要添加新的按鍵可以添加到鍵盤剩下的空白中。下面來講解一下核心代碼的實現(xiàn):

CustomKeyboardView?*keyView?=?[[CustomKeyboardView?alloc]?initWithFrame:CGRectMake(0,?0,?SCREEN_WIDTH,?176)];

self.xTextField.inputView?=?keyView;

-?(instancetype)initWithFrame:(CGRect)frame

{

????self?=?[super?initWithFrame:frame];

????if?(self)?{

????????self.backgroundColor?=?UIColor.lightGrayColor;

????????//創(chuàng)建數(shù)字?jǐn)?shù)組,并打亂

????????NSMutableArray?*integers?=?[[NSMutableArray?alloc]?init];

????????for?(NSInteger?i?=?0;?i?<?10;?++i)?{

????????????[integers?addObject:@(i)];

????????}

????????NSArray??*shuffledIntegers?=?[self?shuffle:integers];

????????//添加數(shù)字按鈕

????????for?(NSInteger?index?=?0;?index?<?10;?++index)?{

????????????UIButton?*btn?=?[UIButton?buttonWithType:UIButtonTypeCustom];

????????????btn.backgroundColor?=?UIColor.grayColor?;

????????????[btn?addTarget:self?action:@selector(buttonDidClicked:)?forControlEvents:UIControlEventTouchUpInside];

????????????btn.frame?=?CGRectMake(CGRectGetWidth(self.frame)?/?3?*?(index?%?3?),?45?*??(index?/?3?)?,?CGRectGetWidth(self.frame)?/?3?-?1,?44);

????????????NSString?*indexString?=?[NSString?stringWithFormat:@"%@",shuffledIntegers[index]];

????????????[btn?setTitle:indexString?forState:UIControlStateNormal];

????????????[self?addSubview:btn];

????????}


????}

????return?self;

}

打亂鍵盤中數(shù)字的布局方法很多,一般的洗牌算法就可以實現(xiàn):

-(NSArray?*)shuffle:(NSArray?*)array

{

????if(array?==?nil?||?array.count?<?1)

????????return?nil;

????NSMutableArray?*resultArray?=?[NSMutableArray?arrayWithArray:array];

????NSInteger?value;

????NSNumber?*median;


????for(NSInteger?index?=?0;?index?<?array.count;?index?++){

????????value?=?rand()?%?resultArray.count;

????????median?=?resultArray[index];


????????resultArray[index]?=?resultArray[value];

????????resultArray[value]?=?median;

????}

????return?resultArray;

}

最后是點擊鍵盤按鈕后的回調(diào)處理

-(void)buttonDidClicked:(UIButton?*)?sender{

????if?(self.delegate)?{

????????[self.delegate?keyboardItemDidClicked:sender.titleLabel.text];

????}

}

相比較而言,大家是否覺得這種方法比第一種方法既簡單又簡潔?

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容