iOS 自定義鍵盤字母按鈕

weixin_34128411發表於2018-11-27

最近專案有個需求24個按鈕, 排列像鍵盤一樣,獲取字母進行索引搜尋單詞, 那就for迴圈新增 按鈕吧, 這是封裝的view, 在這裡記錄一下, 可以直接複製程式碼效果圖

6056059-532ef1144aacd144.png

:

.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);

    }

}

相關文章