UI:自定义键盘的实现

自定义我的封装键盘,并在试图控制器里对接 (解决多 输入框问题,把输入框存入到可变数组)

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    CSKeyBoardViewController * RootVC = [[CSKeyBoardViewController alloc]init];
    self.window.rootViewController = RootVC;
    [RootVC release];
    return YES;
}
View Code main.m
//
//  CSKeyBoardViewController.m
//
#import "CSKeyBoardViewController.h"
#import "KeyBoard.h"

@interface CSKeyBoardViewController ()<UITextFieldDelegate>
@property(nonatomic,retain)UITextField * tf,*tf1,*tf2;
@property(nonatomic,retain)KeyBoard * keyboard;
@property(nonatomic,retain)NSTimer * timer;
@property(nonatomic,retain)NSMutableArray * tfArr;

@end

@implementation CSKeyBoardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UITextField * textfield = [[UITextField alloc]initWithFrame:CGRectMake(10, 20, self.view.frame.size.width-20, 30)];
    NSLog(@"外面%@",textfield);
    textfield.placeholder = @"请输入资料";
    [self.view addSubview:textfield];
    textfield.layer.borderWidth = 1;
    textfield.layer.cornerRadius = 3;
    textfield.delegate = self;
  
    //如果控制器里有好多的 tf 怎么办 <尚未解决 ????????????????>
//    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(textFieldDidBeginEditing:) userInfo:nil repeats:NO];
//    新问题 timer 不知道怎么关闭
    UITextField * textfield2 = [[UITextField alloc]initWithFrame:CGRectMake(10, 70, self.view.frame.size.width-20, 30)];
    NSLog(@"外面2%@",textfield2);
    textfield2.placeholder = @"请输入资料";
    [self.view addSubview:textfield2];
    textfield2.layer.borderWidth = 1;
    textfield2.layer.cornerRadius = 3;
    textfield2.delegate = self;
    
    self.tf1 = [[UITextField alloc]initWithFrame:CGRectMake(10, 120, self.view.frame.size.width-20, 30)];
    _tf1.placeholder = @"请输入资料";
    [self.view addSubview:_tf1];
    _tf1.layer.borderWidth = 1;
    _tf1.layer.cornerRadius = 3;
    _tf1.delegate = self;
    
    self.tf2 = [[UITextField alloc]initWithFrame:CGRectMake(10, 170, self.view.frame.size.width-20, 30)];
    _tf2.placeholder = @"请输入资料";
    [self.view addSubview:_tf2];
    _tf2.layer.borderWidth = 1;
    _tf2.layer.cornerRadius = 3;
    _tf2.delegate = self;

    self.tfArr = [NSMutableArray arrayWithCapacity:4];
    [_tfArr addObject:textfield];
    [_tfArr addObject:textfield2];
    [_tfArr addObject:_tf1];
    [_tfArr addObject:_tf2];
    
    
    NSDate * dateNow = [NSDate date];
    NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString * dateString = [formatter stringFromDate:dateNow];
    KeyBoard * keyboard = [[KeyBoard alloc]initWithFrame:CGRectMake(0, 150, 375, 240) accessoryText:dateString TextFieledArr:_tfArr];
    textfield.inputView = keyboard;
    textfield2.inputView = keyboard;
    _tf1.inputView = keyboard;
    _tf2.inputView = keyboard;
    [keyboard release];
    [textfield release];
    [textfield2 release];
    [_tf1 release];
    [_tf2 release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//如果控制器有多个 tf 我们让当前鼠标所在的tf 指向 self.tf
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    self.tf = textField;
    
    //解决多 tf 问题 (属性)
//    self.keyboard = [[KeyBoard alloc]initWithFrame:CGRectMake(0, 150, 375, 240) accessoryText:@"我的键盘" TextFieled:self.tf];

//    NSLog(@"如果控制器有多个 tf 我们让当前鼠标所在的tf 指向 self.tf   %@------%@",self.tf,textField);
    //站换思路 用数组
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"来自视图控制器  移动了鼠标");
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    for (UITextField * tf in self.tfArr) {
        [tf resignFirstResponder];
    }
    [_tf resignFirstResponder];//这里没有什么卵用才用上面的数组释放键盘
    NSLog(@"试图控制器回收键盘");
}

@end
View Code 试图控制器(m)文件 CSKeyBoardViewController.m
#import <UIKit/UIKit.h>

@interface KeyBoard : UIView<UITextFieldDelegate,UIAlertViewDelegate>
//共外界使用的辅助视图
@property(nonatomic,retain)UILabel * accessorView;
//存储当前的输入框
@property(nonatomic,retain)UITextField * tf;

/**
 *  methods for :
 外部调用初始化方法创建自定义键盘视图同时可设置辅助视图显示文字 指定为外部输入框的inputView 同时可指定辅助视图(如需)(.accessoryView)
 
 外部操作的输入框需赋值给该类的tf属性
 */

//初始化自定义键盘视图及设置辅助文字方法
//- (instancetype)initWithFrame:(CGRect)frame accessoryText:(NSString *)text
//                                               TextFieled:(UITextField *)textfield;

- (instancetype)initWithFrame:(CGRect)frame accessoryText:(NSString *)text
                   TextFieledArr:(NSMutableArray *)textfieldArr;;
//..待丰富...


@end
View Code 自定义键盘.h封装好了 KeyBoard.h
//
//  KeyBoard.m

#define RandomColor [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0  blue:arc4random()%256/255.0  alpha:1.0]

#import "KeyBoard.h"

@implementation KeyBoard

-(instancetype)initWithFrame:(CGRect)frame accessoryText:(NSString *)text TextFieledArr:(NSMutableArray *)textfieldArr{
    self = [super initWithFrame:frame];
    if (self) {
     [self setUpCustomViewWithAccessoryText:text TextFieledArr:textfieldArr];
    }
    return self;
}

-(void)setUpCustomViewWithAccessoryText:(NSString *)accesssoryText
                             TextFieledArr:(NSMutableArray *)textfieldArr{
    NSLog(@"里面%@",textfieldArr);
    self.backgroundColor = RandomColor;
    for (UITextField * tf in textfieldArr) {//遍历外面的 textfield
            tf.delegate = self;
    }

    self.tf.delegate = self;
    //键盘辅助视图
    self.accessorView = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 30)];
    _accessorView.backgroundColor = RandomColor;
    _accessorView.text = accesssoryText;
    _accessorView.textAlignment = NSTextAlignmentCenter;
    _accessorView.textColor = RandomColor;
    [self addSubview:_accessorView];
    //自定义按钮事件
    NSArray * KeyArr = @[@[@"1",@"2",@"3"],@[@"4",@"5",@"6"],@[@"7",@"8",@"9"],@[@"✔️", @"0", @"?"]];
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 3; j++) {
            UIButton * numBUtton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            numBUtton.frame = CGRectMake(10+j*((self.frame.size.width - 20)/3),40+50*i, (self.frame.size.width - 30)/3, 40);
            [numBUtton setTitle:KeyArr[i][j] forState:UIControlStateNormal];
            numBUtton.backgroundColor = RandomColor;
            [numBUtton setTitleColor:RandomColor forState:UIControlStateSelected];
            [numBUtton setTitleColor:RandomColor forState:UIControlStateNormal];
            if (3==i && 0==j) {
                [numBUtton addTarget:self action:@selector(handleDoneAction:) forControlEvents:UIControlEventTouchUpInside];
            }else if (3 == i && 2 == j){
                [numBUtton addTarget:self action:@selector(hanleClearAction:) forControlEvents:UIControlEventTouchUpInside];
            }else{
                [numBUtton addTarget:self action:@selector(handleOtherAction:) forControlEvents:UIControlEventTouchUpInside];
            }
            [self addSubview:numBUtton];
        }
    }
}
//完成输入回收键盘
-(void)handleDoneAction:(UIButton * )sender{
    [self.tf resignFirstResponder];
    NSLog(@"里面 完成输入回收键盘");
}
//删除输入的一个字符
-(void)hanleClearAction:(UIButton *)sender{
    if (self.tf.text.length != 0) {
        NSMutableString * str = (NSMutableString *)self.tf.text;
        NSMutableString *strr = (NSMutableString *)[str substringToIndex:self.tf.text.length - 1];
        self.tf.text = strr;
//        NSRange range = NSMakeRange(self.tf.text.length - 1, 1);//取出最后一个字符串
//        [str  deleteCharactersInRange:range];//删除最后一个字符串
//        self.tf.text =  str;
        NSLog(@"删除输入的一个字符%@",str);
    }
}
//编辑的时候进行保存
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    self.tf = textField;
    NSLog(@"编辑的时候进行保存%@  %@",self.tf.text,self.tf);
}
//其他按钮
-(void)handleOtherAction:(UIButton *)sender{
    NSMutableString * string = [NSMutableString stringWithFormat:@"%@",self.tf.text];
    NSString * butText = [sender titleForState:UIControlStateNormal];
    [string appendString:butText];
    self.tf.text = string;
    NSLog(@"其他按钮%@",string);
}
//空白处回收键盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.tf  resignFirstResponder];//然而这里是按住 了键盘的空白处才回收
    NSLog(@"来自封装类 空白处回收键盘");
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"来自封装类 移动了鼠标");
}
@end
View Code 自定义键盘.m封装好了 KeyBoard.m

 

参考1 参考2 参考3

3.自定义封装我的相册页面(待完善)

4.自定义封装我的调色板页面(待完善)

转载于:https://www.cnblogs.com/benpaobadaniu/p/4782714.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值