iOS系統(tǒng)提供了多種鍵盤(pán)阱州,我們可以通過(guò)Enum類(lèi)型設(shè)置。但有的時(shí)候由于某些特殊業(yè)務(wù)的需要勋拟,我們不得不自定義鍵盤(pán)勋磕,比如某些銀行的APP處于安全考慮,他們鍵盤(pán)數(shù)字的位置是隨機(jī)的敢靡,這個(gè)時(shí)候只能自定義鍵盤(pán)挂滓。幸運(yùn)的是,iOS也為我們提供了多種方式自定義鍵盤(pán)醋安。我們可以根據(jù)自身情況選擇合適的方案杂彭。
typedef?NS_ENUM(NSInteger,?UIKeyboardType)?{
????UIKeyboardTypeDefault,
????UIKeyboardTypeASCIICapable,?can?enter?ASCII?characters
????UIKeyboardTypeNumbersAndPunctuation,
????UIKeyboardTypeURL,
????UIKeyboardTypeNumberPad,
????UIKeyboardTypePhonePad,
????UIKeyboardTypeNamePhonePad,
????UIKeyboardTypeEmailAddress,
????UIKeyboardTypeDecimalPad?,
????UIKeyboardTypeTwitter?,
????UIKeyboardTypeWebSearch?,
????UIKeyboardTypeASCIICapableNumberPad?,
????UIKeyboardTypeAlphabet?=?UIKeyboardTypeASCIICapable,
};
對(duì)現(xiàn)有鍵盤(pán)稍加改動(dòng)
這種情況適合身份證的輸入墓毒。由于身份證大部分情況下都是數(shù)字吓揪,偶爾可能出現(xiàn)“X”字符,如果我們棄用數(shù)字鍵盤(pán)直接使用諸如UIKeyboardTypeNumbersAndPunctuation等包含數(shù)字和字符的鍵盤(pán)又顯得沒(méi)有太大必要所计,那稍加改動(dòng)數(shù)字鍵盤(pán)是個(gè)不錯(cuò)的選擇柠辞。思路也很簡(jiǎn)單,在彈出鍵盤(pán)的同時(shí)獲取鍵盤(pán)對(duì)應(yīng)的window主胧,在window上加上我們需要的按鈕即可叭首。如下是筆者改動(dòng)的數(shù)字鍵盤(pán)。
下面踪栋,我對(duì)重點(diǎn)代碼做個(gè)講解:
-(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;
????//以下是對(duì)添加的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];
????//獲取鍵盤(pán)對(duì)應(yīng)的Window(這段代碼只在iOS?11上做過(guò)測(cè)試,還不夠嚴(yán)謹(jǐn))
????UIWindow?*tempWindow?=?[[[UIApplication?sharedApplication]?windows]?lastObject];
????[tempWindow?addSubview:self.extrakeyButton];
????//設(shè)置動(dòng)畫(huà)
????[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 上做過(guò)測(cè)試夷都,可能不具備一定的普遍性眷唉,比如iPhone X的鍵盤(pán)位置有一定改變,按鈕的位置需要加代碼兼容囤官。因此這種解決方案非常局限冬阳,只能用于身份證等業(yè)務(wù)非常簡(jiǎn)單的備選方案。有個(gè)iOS開(kāi)發(fā)者對(duì)這個(gè)鍵盤(pán)做了個(gè)封裝党饮,讀者可以點(diǎn)擊這里獲取
自己設(shè)計(jì)一個(gè)鍵盤(pán)
iOS中可以通過(guò)設(shè)置TextField/TextView的inputView來(lái)定制鍵盤(pán),
//The?custom?input?view?to?display?when?the?text?field?becomes?the?first?responder.
@property(readwrite,?strong)?UIView?*inputView;
這個(gè)自定義鍵盤(pán)就解決了我們開(kāi)頭提到的問(wèn)題:鍵盤(pán)上的數(shù)字是隨機(jī)排列的0-9肝陪,如果需要添加新的按鍵可以添加到鍵盤(pán)剩下的空白中。下面來(lái)講解一下核心代碼的實(shí)現(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;
}
打亂鍵盤(pán)中數(shù)字的布局方法很多镶柱,一般的洗牌算法就可以實(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ǎn)擊鍵盤(pán)按鈕后的回調(diào)處理
-(void)buttonDidClicked:(UIButton?*)?sender{
????if?(self.delegate)?{
????????[self.delegate?keyboardItemDidClicked:sender.titleLabel.text];
????}
}
相比較而言,大家是否覺(jué)得這種方法比第一種方法既簡(jiǎn)單又簡(jiǎn)潔涯呻?