码迷,mamicode.com
首页 > 其他好文 > 详细

自定义键盘~新手看

时间:2016-11-08 17:22:43      阅读:317      评论:0      收藏:0      [点我收藏+]

标签:提醒   header   tor   cts   war   回调   文本   float   继承   

写这日志以及前面的一些简单的东西就是 纯粹的重温一下 记录下自己的点点滴滴  从runtime runloop 到 函数底层 可能是年纪大了 想回顾一下 从入行到现在本人学了java Python PHP Scala C# JS等等一系列的语言  并学以致用 希望以后能有更好的风景~~ 这篇日志很适合新手看 大神勿喷

自定义键盘 应用场景 密码输入

创建 

CustomTextField 继承于UITextField

KeyBoardView 继承于UIView

技术分享

CustomTextField.h

@interface CustomTextField : UITextField

/*
 指定区域,提醒文字,左视图图片名字
 */
- (instancetype)initWithFrame:(CGRect)frame
               withPlacHolder:(NSString *)holder
            withLeftImageName:(NSString *)name;


@end

 CustomTextField.m

#import "CustomTextField.h"

@interface CustomTextField ()
///用来记录图片名字的属性
@property (copy, nonatomic) NSString *leftImagName;
///用来记录提醒文字(水印)的属性
@property (copy, nonatomic) NSString *textHolder;
///左视图
@property (strong, nonatomic) UIImageView *leftImageView;
@end


@implementation CustomTextField

- (instancetype)initWithFrame:(CGRect)frame
               withPlacHolder:(NSString *)holder
            withLeftImageName:(NSString *)name{
    //记录相关名字
    self.leftImagName = name;
    self.textHolder = holder;
    return [self initWithFrame:frame];
}
/*
  进行初始化设置
 */
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        ///当前文本框的基本配置(leftView)
        //编辑的时候,显示清除按钮
        self.clearButtonMode = UITextFieldViewModeWhileEditing;
        //设置当前文本框的背景图
        self.background = [UIImage imageNamed:@"background"];
        //设置提醒文字
        self.placeholder = self.textHolder;
        //设置左视图
        self.leftView =  self.leftImageView;
        //设置左视图呈现的形式(默认不出现)
        self.leftViewMode =  UITextFieldViewModeAlways;
    }
    return self;
}

#pragma mark
#pragma mark  Attribute

- (UIImageView *)leftImageView{
    if (!_leftImageView) {
        _leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 35, 35)];
        [_leftImageView setImage:[UIImage imageNamed:self.leftImagName]];
        //内容填充的方式(默认的拉伸填充)
        _leftImageView.contentMode = UIViewContentModeCenter;
    }
    return _leftImageView;
}




@end

KeyBoardView.h

#import <UIKit/UIKit.h>
@class KeyBoardView;

@protocol KeyBoardViewDelegate <NSObject>
//代理方法,用来回传点击按钮上的数据
- (void)sendMessageInView:(KeyBoardView *)bView
              backContent:(NSString *)content;
@end

//1
typedef void(^Block)(KeyBoardView *,NSString *);


@interface KeyBoardView : UIView
///代理对象
@property (assign, nonatomic) id <KeyBoardViewDelegate>delegate;
///2block 属性
@property (copy, nonatomic) Block myBlock;

//给myBlock指向一个代码块的方法
- (void)viewWithBlock:(Block)inBlock;


@end

KeyBoardView.m

#import "KeyBoardView.h"

static CGFloat const kMargin = 20;
static NSUInteger const kBtnCountInRow = 3;
static CGFloat const kBtnHeight = 35;

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

@interface KeyBoardView ()

@property (copy, nonatomic) NSArray *arrBtnTitles;

@end

@implementation KeyBoardView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        CGFloat btnWidth = (SCREEN_WIDTH - (kMargin * (kBtnCountInRow + 1)))/kBtnCountInRow;
        
        //循环创建按钮,添加到当前视图中
        for (int i = 0; i < self.arrBtnTitles.count; i++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            //设置按钮标题
            [btn setTitle:self.arrBtnTitles[i] forState:UIControlStateNormal];
            //
            int x = i % kBtnCountInRow;
            //
            int y = i / kBtnCountInRow;
            //设置按钮的区域
            [btn setFrame:CGRectMake(kMargin + x *(kMargin + btnWidth), kMargin + y * (kMargin + kBtnHeight), btnWidth, kBtnHeight)];
            //给按钮设置个颜色
            [btn setBackgroundColor:[UIColor orangeColor]];
            //给按钮标题设置颜色
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            //给按钮关联事件
            [btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
            //将按钮加入到视图中
            [self addSubview:btn];
        }
    }
    return self;
}
///按钮关联的事件
- (void)btnPressed:(UIButton *)btn{
    [UIView animateWithDuration:0.1 animations:^{
        btn.alpha = 1.0;
        btn.alpha = 0.2;
        btn.alpha = 1.0;
//        btn.transform = CGAffineTransformMakeScale(2.5, 2.5);
//        btn.transform = CGAffineTransformMakeScale(1.0, 1.0);
    }];
    
    NSLog(@"%@",btn.titleLabel.text);
    //判断代理对象是否响应方法,如果响应才调用
//    if ([self.delegate respondsToSelector:@selector(sendMessageInView:backContent:)]) {
//        [self.delegate sendMessageInView:self backContent:btn.titleLabel.text];
//    }
    //使用block进行数据回传(调用myBlock)
    self.myBlock(self,btn.titleLabel.text);
    //给按钮执行动画
    
}

///让myBlock指向一个代码块
- (void)viewWithBlock:(Block)inBlock{
    self.myBlock = inBlock;
}


#pragma mark
#pragma mark  Attribute
///存储按钮标题
- (NSArray *)arrBtnTitles{
    if (!_arrBtnTitles) {
        _arrBtnTitles = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"#",@"0",@"*", nil];
    }
    return _arrBtnTitles;
}

然后在ViewController.m调用就可以了 

#import "ViewController.h"
#import "CustomTextField.h"
#import "KeyBoardView.h"

@interface ViewController ()<KeyBoardViewDelegate,UITextFieldDelegate>
///自定义文本框
@property (strong, nonatomic) CustomTextField *tfName;
///自定义键盘
@property (strong, nonatomic) KeyBoardView *cusInputView;
///记录文本内容的字符串
@property (strong, nonatomic) NSMutableString *textFieldContents;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.tfName];
    //接受block回传的数据
    [self.cusInputView viewWithBlock:^(KeyBoardView *kView, NSString *content) {
        //将回传的数据追加
        [self.textFieldContents appendString:content];
        //设置tfName的文本内容
        self.tfName.text = self.textFieldContents;
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

#pragma mark
#pragma mark  delegate
// 协议中的方法
- (void)sendMessageInView:(KeyBoardView *)bView backContent:(NSString *)content{
    //将回传的数据追加
    [self.textFieldContents appendString:content];
    //设置tfName的文本内容
    self.tfName.text = self.textFieldContents;
}
/*
 点击清除按钮的时候,该方法回调
 */
- (BOOL)textFieldShouldClear:(UITextField *)textField{
    //原来记录内容的字符串给空值
    [self.textFieldContents replaceCharactersInRange:NSMakeRange(0, self.textFieldContents.length) withString:@""];
    return YES;
}

#pragma mark
#pragma mark  Attribute
- (CustomTextField *)tfName{
    if (!_tfName) {
        _tfName = [[CustomTextField alloc]initWithFrame:CGRectMake(0, 80, CGRectGetWidth(self.view.frame), 45) withPlacHolder:@"输入名字" withLeftImageName:@"userName"];
        //设置文本框的inputView(自定义键盘)
        _tfName.inputView = self.cusInputView;
        //设置代理(用来点击清除按钮的时候,回调)
        _tfName.delegate = self;
        //inputAccessoryView
        
        UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 40)];
        headerView.backgroundColor = [UIColor blackColor];
        _tfName.inputAccessoryView = headerView;
        
        
    }
    return _tfName;
}
- (KeyBoardView *)cusInputView{
    if (!_cusInputView) {
        _cusInputView = [[KeyBoardView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 250)];
        //设置代理对象
        _cusInputView.delegate = self;
    }
    return _cusInputView;
}

- (NSMutableString *)textFieldContents{
    if (!_textFieldContents) {
        _textFieldContents = [NSMutableString string];
    }
    return _textFieldContents;
}



@end

是不是 Very simple~~

自定义键盘~新手看

标签:提醒   header   tor   cts   war   回调   文本   float   继承   

原文地址:http://www.cnblogs.com/liujiancheng/p/6043149.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!