iOS 自定義鍵盤字母按鈕
最近專案有個需求24個按鈕, 排列像鍵盤一樣,獲取字母進行索引搜尋單詞, 那就for迴圈新增 按鈕吧, 這是封裝的view, 在這裡記錄一下, 可以直接複製程式碼效果圖
:
.h 裡有個block 可以回撥點選的是哪個單詞這裡床的是字串
#import
@interfaceTKYKeyboardView :UIView
@property(nonatomic,copy)void(^seachLetterStrbBlock)(NSString* letterStr);
@end
.m中具體思路是建立三個view, 第一行是一個view 第二行是一個view, 第三行字母也是一個view,然後for迴圈 新增字母按鈕
#import "TKYKeyboardView.h"
#import "UIView+Extension.h"
@interface TKYKeyboardView()
@property (nonatomic, strong)NSMutableArray *arrM;
@property (nonatomic, strong)UIButton *button;
@end
@implementationTKYKeyboardView
- (instancetype)initWithFrame:(CGRect)frame {
if(self= [superinitWithFrame:frame]) {
[selfsetUI];
}
return self;
}
- (void)setUI{
CGFloatspace =5;
CGFloatx =0;
self.arrM = [NSMutableArray array];
UIView *oneView = [[UIView alloc]initWithFrame:CGRectMake(20, 109, [UIScreen mainScreen].bounds.size.width-40, 30)];
// oneView.backgroundColor = [UIColor yellowColor];
[selfaddSubview:oneView];
NSArray *oneArr = @[@"Q",@"W",@"E",@"R",@"T",@"Y",@"U",@"I",@"O",@"P"];
for(inti=0; i
{
x =i*((oneView.kx_width-9*5)/10) +i*space;
UIButton *pressButton =[UIButton buttonWithType:UIButtonTypeCustom];
[pressButtonsetBackgroundImage:[UIImage imageNamed:@"tu1 (2).png"] forState:UIControlStateNormal];
[pressButtonsetBackgroundImage:[UIImage imageNamed:@"tu1 2.png"] forState:UIControlStateHighlighted];
pressButton.tag= i;
pressButton.backgroundColor = [UIColor redColor];
pressButton.frame=CGRectMake(x,0, (oneView.kx_width-9*5)/10, ((oneView.kx_width-9*5)-9*5)/10);
[self.arrMaddObject:pressButton];
pressButton.selected=NO;
[pressButtonsetTitle:oneArr[i] forState:UIControlStateNormal];
[pressButtonaddTarget:self action:@selector(goPressSecondKeyButtons:) forControlEvents:UIControlEventTouchUpInside];
[oneViewaddSubview:pressButton];
}
NSArray *twoArr = @[@"A",@"S",@"D",@"F",@"G",@"H",@"J",@"K",@"L"];
UIView *twoView = [[UIView alloc]initWithFrame:CGRectMake(40, oneView.kx_bottom+space, [UIScreen mainScreen].bounds.size.width-80, 30)];
twoView.backgroundColor = [UIColor yellowColor];
[selfaddSubview:twoView];
for(inti=0; i
{
x =i*((twoView.kx_width-8*5)/9) +i*space;
UIButton *pressButton =[UIButton buttonWithType:UIButtonTypeCustom];
[pressButtonsetBackgroundImage:[UIImage imageNamed:@"tu1 (2).png"] forState:UIControlStateNormal];
[pressButtonsetBackgroundImage:[UIImage imageNamed:@"tu1 2.png"] forState:UIControlStateHighlighted];
pressButton.tag= i+200;
pressButton.backgroundColor = [UIColor redColor];
pressButton.frame=CGRectMake(x,0, (twoView.kx_width-8*space)/9, ((twoView.kx_width-8*space)-8*5)/9);
[self.arrMaddObject:pressButton];
pressButton.selected=NO;
[pressButtonsetTitle:twoArr[i] forState:UIControlStateNormal];
[pressButtonaddTarget:self action:@selector(goPressSecondKeyButtons2:) forControlEvents:UIControlEventTouchUpInside];
[twoViewaddSubview:pressButton];
}
NSArray *threeArr = @[@"Z",@"X",@"C",@"V",@"B",@"N",@"M"];
CGFloat threeView_with = [UIScreen mainScreen].bounds.size.width-160;
UIView *threeView = [[UIView alloc]initWithFrame:CGRectMake(80, twoView.kx_bottom+space, [UIScreen mainScreen].bounds.size.width-160, 30)];
[selfaddSubview:threeView];
for(inti=0; i
{
x =i*((threeView_with-6*space)/7) +i*space;
UIButton *pressButton =[UIButton buttonWithType:UIButtonTypeCustom];
[pressButtonsetBackgroundImage:[UIImage imageNamed:@"tu1 (2).png"] forState:UIControlStateNormal];
[pressButtonsetBackgroundImage:[UIImage imageNamed:@"tu1 2.png"] forState:UIControlStateHighlighted];
pressButton.tag= i+300;
pressButton.backgroundColor = [UIColor redColor];
pressButton.frame=CGRectMake(x,0, (threeView_with-6*space)/7, ((threeView_with-6*space)-6*space)/7);
[self.arrMaddObject:pressButton];
pressButton.selected=NO;
[pressButtonsetTitle:threeArr[i]forState:UIControlStateNormal];
[pressButtonaddTarget:self action:@selector(goPressSecondKeyButtons3:) forControlEvents:UIControlEventTouchUpInside];
[threeViewaddSubview:pressButton];
}
}
- (void)goPressSecondKeyButtons:(UIButton*)button{
for(UIButton*btninself.arrM) {
if(btn.tag== button.tag) {
btn.backgroundColor = [UIColor grayColor];
}else{
btn.backgroundColor = [UIColor redColor];
}
}
if (self.seachLetterStrbBlock) {
self.seachLetterStrbBlock(button.titleLabel.text);
}
}
- (void)goPressSecondKeyButtons2:(UIButton*)button{
for(UIButton*btninself.arrM) {
if(btn.tag== button.tag) {
btn.backgroundColor = [UIColor grayColor];
}else{
btn.backgroundColor = [UIColor redColor];
}
}
if (self.seachLetterStrbBlock) {
self.seachLetterStrbBlock(button.titleLabel.text);
}
}
- (void)goPressSecondKeyButtons3:(UIButton*)button{
for(UIButton*btninself.arrM) {
if(btn.tag== button.tag) {
btn.backgroundColor = [UIColor grayColor];
}else{
btn.backgroundColor = [UIColor redColor];
}
}
if (self.seachLetterStrbBlock) {
self.seachLetterStrbBlock(button.titleLabel.text);
}
}
相關文章
- LabVIEW的自定義按鈕View
- Qt QMessageBox::information 自定義按鈕QTORM
- Simple WPF: WPF 自定義按鈕外形
- 如何自定義radio按鈕的樣式
- android 自定義鍵盤Android
- vue自定義鍵盤Vue
- 自定義Android鍵盤Android
- 自定義鍵盤(二)
- 自定義鍵盤(一)
- Qt自定義開關按鈕控制元件QT控制元件
- 「HTML+CSS」--自定義按鈕樣式【003】HTMLCSS
- 「HTML+CSS」--自定義按鈕樣式【002】HTMLCSS
- 「HTML+CSS」--自定義按鈕樣式【004】HTMLCSS
- 「HTML+CSS」--自定義按鈕樣式【001】HTMLCSS
- antd-mobile 自定義picker按鈕樣式
- android自定義view(自定義數字鍵盤)AndroidView
- C#自定義控制元件—旋轉按鈕C#控制元件
- Windows API視窗程式設計 - 自定義按鈕WindowsAPI程式設計
- fastadmin新增自定義按鈕,並使用彈窗功能AST
- 自定義有多個按鈕節點的SliderViewIDEView
- Android自定義數字鍵盤Android
- 鍵盤大寫字母按哪個鍵 轉換英文字母大小寫是哪個鍵
- (五)自定義按鈕模板和設定觸發器觸發器
- 如何給 SAP Fiori Elements 應用新增自定義按鈕
- Windows API視窗程式設計 - 完善自定義按鈕WindowsAPI程式設計
- 自定義按鈕 圖片標題位置隨意放置
- ASPxGridView中Command列自定義按鈕點選事件概要View事件
- Swift自定義表情鍵盤+錄音Swift
- Android自定義view之emoji鍵盤AndroidView
- JS判斷鍵盤是否按的Enter鍵並觸發指定按鈕點選操作JS
- 鍵盤按鍵錯亂怎麼恢復 電腦鍵盤字母錯亂三種解決方法
- 直播平臺搭建原始碼,qt自定義滑動按鈕原始碼QT
- 直播系統程式碼,自定義軟鍵盤樣式:字母、數字、標點三種切換
- Vue2-利用自定義指令實現按鈕許可權控制Vue
- laravel admin 列展開 自定義行操作 增加編輯修改按鈕Laravel
- 基於VUE自定義指令實現按鈕級許可權控制Vue
- Yii1自定義 CGridView 中的操作按鈕中 CButtonColumn 選項View
- 使用自定義 View 繪製一個懸浮式可拖拽按鈕View