`
gaobusi
  • 浏览: 199619 次
  • 性别: Icon_minigender_1
  • 来自: 安徽
社区版块
存档分类
最新评论

iphone自定义键盘优化版(数字键盘+字母键盘)

    博客分类:
  • IOS
阅读更多

  一直往下拉有截图,忍不住啰嗦的朋友看图先!

 

  以前有篇文章发了个键盘,现在这个把代码和功能优化了些许,介绍一下键盘功能:

 

 (1)支持数字、小写字母、大写字母切换


 (2)实现长按删除键可持续删除(用了个定时器,为了实现这个功能,可是煞费苦心呐,呵呵)


 (3)可预设置默认出现的是数字键盘还是字母键盘(当然稍微修改可以控制只能输入数字或只能输入字母)


 (4)可预设置是否是密码输入框


 (5)稍作修改可用于ipad版本上,用弹出框弹出键盘

 

   现在这个键盘还有很高的优化空间,由于现在本人没时间和精力就弄到这,足够现在的项目用了,下载的朋友如果有时间可以给优化下顺便给俺留个链接地址(谢谢撒),个人觉得不会太难,实现成跟苹果键盘的显示完全一样应该不是很困难,我第一个想做的是把下面代码中提到的“套子”给灭了,不过马上要工作。

 

   哥们喜欢直接上代码或直接上图片,下面介绍一个简单的实例:

 

第一步,新建一个ViewController(带xib的),拖四个UITextField,建四个变量并且关联上,在h文件中实现UITextFieldDelegate,WDLKeyboardDelegate这个两个代理。

 

 

//
//  ViewController.h
//  WDLKeyboard
//
//  Created by apple on 12-6-6.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "WDLKeyboard.h"

@interface ViewController : UIViewController<UITextFieldDelegate,WDLKeyboardDelegate>{
    
    IBOutlet UITextField *text_1;//默认显示数字键盘
    
    IBOutlet UITextField *text_2;//默认显示小写字母键盘
    
    IBOutlet UITextField *text_3;//默认显示大写字母键盘
    
    IBOutlet UITextField *text_4;//默认数字密码键盘,且最多输入10位字符
    
    int oldTextTag;
    
}

@end

 

 

 第二步,在m文件中配置textField的delegate和WDLKeyboardDelegate

 

 

//
//  ViewController.m
//  WDLKeyboard
//
//  Created by apple on 12-6-6.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ViewController.h"
#import "WDLKeyboard.h"

@implementation ViewController

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle

- (void)viewDidLoad {
    
    //设置代理
    text_1.delegate = self;
    text_2.delegate = self;
    text_3.delegate = self;
    text_4.delegate = self;
    //设置标签
    [text_1 setTag:1];
    [text_2 setTag:2];
    [text_3 setTag:3];
    [text_4 setTag:4];
    
    [super viewDidLoad];
}

- (void)viewDidUnload {
    [super viewDidUnload];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return NO;
}

#pragma mark -  UITextFieldDelegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    if (textField.tag == 1) {
        WDLKeyboard *keyboard = [[WDLKeyboard alloc]initWithFrame:CGRectZero keyboardType:KeyboardTypeNumKeyPad secureType:NO];
        [keyboard setDelegate:self];
        [keyboard showKeyboardView:textField.text];
    } else if (textField.tag == 2) {
        WDLKeyboard *keyboard = [[WDLKeyboard alloc]initWithFrame:CGRectZero keyboardType:KeyboardTypeCharKeyPad secureType:NO];
        [keyboard setDelegate:self];
        [keyboard showKeyboardView:textField.text];
    } else if (textField.tag == 3) {
        WDLKeyboard *keyboard = [[WDLKeyboard alloc]initWithFrame:CGRectZero keyboardType:KeyboardTypeUpperKeyCharPad secureType:NO];
        [keyboard setDelegate:self];
        [keyboard showKeyboardView:textField.text];
    } else if (textField.tag == 4) {
        WDLKeyboard *keyboard = [[WDLKeyboard alloc]initWithFrame:CGRectZero keyboardType:KeyboardTypeNumKeyPad secureType:YES];
        [keyboard setKeyboardValueMaxLength:10];
        [keyboard setDelegate:self];
        [keyboard showKeyboardView:textField.text];
    }
    
    //加个背景用于区别
    [textField setBackgroundColor:[UIColor lightGrayColor]];
    
    //记录当前textFiled的标签,下面的keyboard代理中使用
    oldTextTag = textField.tag;
    
    //返回NO将不会调出系统键盘
    return NO;
}

#pragma mark - WDLKeyboardDelegate Methods

-(void)wdlkeyboardView:(WDLKeyboard*)keyboardView doneWithValue:(NSString *)value{
    UITextField *textField = (UITextField*)[self.view viewWithTag:oldTextTag];
    [textField setText:value];
    //恢复背景
    [textField setBackgroundColor:[UIColor whiteColor]];
}

@end
 

 

第三部,啥也不啥,贴出来WDLKeyboard这个类,自己看,注释都有滴。

 

 

//
//  WDLKeyboard.h
//  wdlKeyboard1
//
//  Created by wdl on 12-4-18.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//


#define KeyboardView_Frame CGRectMake(0, 0, 320, 260)

#define Num_FontSize 28
#define Char_FontSize 24
#define Small_FontSize 18

#define But_TextFontColor [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0]
#define But_TextFontColor_Highlight [UIColor colorWithRed:0 green:0 blue:0 alpha:1.0]

#define num_w 105
#define num_h 53
#define num_top 44
#define char_w 45
#define char_h 53
#define char_top 44

#define KBKey_Highlight @"KBKey_Highlight.png"
#define KBKey_Default @"KBKey_Default.png"
#define KBKey_Del_Default_1 @"KBKey_Del_Default_1.png"
#define KBKey_Del_Highlight_1 @"KBKey_Del_Highlight_1.png"
#define KBKey_Del_Default @"KBKey_Del_Default.png"
#define KBKey_Del_Highlight @"KBKey_Del_Highlight.png"

typedef enum{
	KeyboardTypeNumKeyPad = 0,          //字母键盘
	KeyboardTypeCharKeyPad = 1,         //小写键盘
	KeyboardTypeUpperKeyCharPad = 2,	//大写键盘
}KeyboardTypeEnum;

#import <UIKit/UIKit.h>

@protocol WDLKeyboardDelegate;

@interface WDLKeyboard : UIView{
    
    KeyboardTypeEnum keyboardType; // 键盘类型
    
    id<WDLKeyboardDelegate>delegate; //代理
    
    NSString *keyboardValue; //textField当前值
    
    NSString *defaultValue; //textField初始值
    
	NSInteger keyboardValueMaxLength; //最大长度
    
    NSArray *numArray; //数字数字
    
    NSArray *charArray; //小写字母数字
    
    NSArray *upperCharArray; //大写字母数字
    
    UIActionSheet *_actionSheet;
	
    UIToolbar *_topToolBar;
	
    UITextField *_txtInput;
    
    NSTimer  *_timer; //定时器
}

@property (nonatomic) KeyboardTypeEnum keyboardType;

@property (nonatomic, assign) id<WDLKeyboardDelegate>delegate;

@property (nonatomic, retain) NSString *keyboardValue;

@property (nonatomic, retain) NSString *defaultValue;

@property (nonatomic) NSInteger keyboardValueMaxLength;

@property (nonatomic, retain) NSArray *numArray;

@property (nonatomic, retain) NSArray *charArray;

@property (nonatomic, retain) NSArray *upperCharArray;

@property (nonatomic, retain, readonly) NSTimer *timer;

-(id)initWithFrame:(CGRect)frame keyboardType:(KeyboardTypeEnum)kType secureType:(BOOL) sType;

-(void)CustomKeyboardViewDismiss;

-(void)CustomKeyboardViewSubmit;

-(void)showKeyboardView:(NSString*)inputValue;

-(IBAction)keyPress:(id)sender;

-(void)changeKeyboardType;

-(IBAction)delKeyPressBegin:(id)sender;

-(IBAction)delKeyPressEnd:(id)sender;

-(void)delTextValue;

@end

@protocol WDLKeyboardDelegate

@required

//代理
-(void)wdlkeyboardView:(WDLKeyboard*)keyboardView doneWithValue:(NSString *)value;

@end
 

 

 

//
//  WDLKeyboard.m
//  wdlKeyboard1
//
//  Created by wdl on 12-4-18.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "WDLKeyboard.h"

@implementation WDLKeyboard
@synthesize keyboardType;
@synthesize delegate;
@synthesize keyboardValue;
@synthesize defaultValue;
@synthesize keyboardValueMaxLength;
@synthesize numArray;
@synthesize charArray;
@synthesize upperCharArray;
@dynamic timer;

#pragma mark -
#pragma mark timer

- (NSTimer *)timer {
	return _timer;
}

- (void)setTimer:(NSTimer *)newTimer {
	[_timer invalidate];
	[newTimer retain];
	[_timer release];
	_timer = newTimer;
}

-(void)stopTimer{
	self.timer = nil;
}

- (id)initWithFrame:(CGRect)frame keyboardType:(KeyboardTypeEnum)kType secureType:(BOOL) sType{
    self = [super initWithFrame:frame];
    if (self) {
        [self setOpaque:NO];
		[self setBackgroundColor:[UIColor blackColor]];
		[self setFrame:KeyboardView_Frame];
        
        //数组定义
        numArray = [[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"abc",@"0",@" ",nil];
        charArray = [[NSArray alloc]initWithObjects:@"a",@"b",@"c",@"d",@"e",
                     @"f",@"g",@"h",@"i",@"j",
                     @"k",@"l",@"m",@"n",@"o",
                     @"p",@"q",@"r",@"s",@"t",
                     @"u",@"ABC",@"v",@"w",@"x",@"y",
                     @"z",@" ",nil];
        upperCharArray = [[NSArray alloc]initWithObjects:@"A",@"B",@"C",@"D",@"E",
                          @"F",@"G",@"H",@"I",@"J",
                          @"K",@"L",@"M",@"N",@"O",
                          @"P",@"Q",@"R",@"S",@"T",
                          @"U",@"123",@"V",@"W",@"X",@"Y",
                          @"Z",@" ",nil];
        
        //外套
		NSString *actionSheetTitle = UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation) ? @"\n\n\n\n\n\n\n\n\n\n" : @"\n\n\n\n\n\n\n\n\n\n\n\n\n";
		_actionSheet = [[[UIActionSheet alloc] initWithTitle:actionSheetTitle
                                                    delegate:nil
                                           cancelButtonTitle:nil
                                      destructiveButtonTitle:nil
                                           otherButtonTitles:nil] autorelease];
		
        //文本标签
		CGRect txtInputFrame = CGRectMake( 0, 0, 200, 31);
		_txtInput = [[[UITextField alloc] initWithFrame:txtInputFrame] autorelease];
		[_txtInput setEnabled:NO];
		[_txtInput setTextAlignment:UITextAlignmentLeft];
		[_txtInput setFont:[UIFont systemFontOfSize:16]];
		[_txtInput setBackgroundColor:[UIColor clearColor]];
		[_txtInput setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
		[_txtInput setBorderStyle:UITextBorderStyleRoundedRect];
        //ios4.2和之前的版本UITextField是不透明白色背景,而气体也是白色,所以会造成看不到文本值的先下,下面特别处理了下字体颜色
        Class ios5Class = (NSClassFromString(@"CIImage"));
        if (nil != ios5Class) {
            [_txtInput setTextColor:[UIColor whiteColor]];
        }else{
            [_txtInput setTextColor:[UIColor blackColor]];
        }
        //设置是否是密码键盘
        [_txtInput setSecureTextEntry:sType];
        
		//工具栏
		CGRect topToolBarFrame = CGRectMake( 0, 0, 320, 44);
		_topToolBar = [[[UIToolbar alloc] initWithFrame:topToolBarFrame] autorelease];
		_topToolBar.barStyle = UIBarStyleBlack;
		UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleBordered target:self action:@selector(CustomKeyboardViewDismiss)];
		UIBarButtonItem *spaceItemLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
		UIBarButtonItem *inputItem = [[UIBarButtonItem alloc] initWithCustomView:_txtInput];
		UIBarButtonItem *spaceItemRight = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
		UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStyleDone target:self action:@selector(CustomKeyboardViewSubmit)];
		[_topToolBar setItems:[NSArray arrayWithObjects:cancelItem,spaceItemLeft,inputItem,spaceItemRight,doneItem,nil]];
		[self addSubview:_topToolBar];
        
        //数字键
        for (int i=0; i<12; i++) {
            float kx ;
            float ky ;
            kx = 1 + i%3 * num_w + i%3;
            ky = num_top + i/3 * num_h + i/3;
            UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
            [bt setTag:(1000+i)];
            [bt setFrame:CGRectMake(kx, ky, num_w, num_h)];
            [bt setTitle:[numArray objectAtIndex:i] forState:UIControlStateNormal];
            if (i == 9) {
                [bt addTarget:self action:@selector(keyPress:) forControlEvents:UIControlEventTouchUpInside];
                
                [bt.titleLabel setFont:[UIFont boldSystemFontOfSize:Num_FontSize]];
                [bt setTitleColor:But_TextFontColor_Highlight forState:UIControlStateNormal];
                [bt setTitleColor:But_TextFontColor forState:UIControlStateHighlighted];
                [bt setBackgroundImage:[UIImage imageNamed:KBKey_Highlight] forState:UIControlStateNormal];
                [bt setBackgroundImage:[UIImage imageNamed:KBKey_Default] forState:UIControlStateHighlighted];
            } else if (i == 11){
                [bt addTarget:self action:@selector(delKeyPressBegin:) forControlEvents:UIControlEventTouchDown];  
                [bt addTarget:self action:@selector(delKeyPressEnd:) forControlEvents:UIControlEventTouchUpInside];  
                [bt addTarget:self action:@selector(delKeyPressEnd:) forControlEvents:UIControlEventTouchUpOutside];
                
                [bt.titleLabel setFont:[UIFont boldSystemFontOfSize:Num_FontSize]];
                [bt setTitleColor:But_TextFontColor_Highlight forState:UIControlStateNormal];
                [bt setTitleColor:But_TextFontColor forState:UIControlStateHighlighted];
                [bt setBackgroundImage:[UIImage imageNamed:KBKey_Del_Default] forState:UIControlStateNormal];
                [bt setBackgroundImage:[UIImage imageNamed:KBKey_Del_Highlight] forState:UIControlStateHighlighted];
                
            } else {
                [bt addTarget:self action:@selector(keyPress:) forControlEvents:UIControlEventTouchUpInside];
                
                [bt.titleLabel setFont:[UIFont boldSystemFontOfSize:Num_FontSize]];
                [bt setTitleColor:But_TextFontColor forState:UIControlStateNormal];
                [bt setTitleColor:But_TextFontColor_Highlight forState:UIControlStateHighlighted];
                [bt setBackgroundImage:[UIImage imageNamed:KBKey_Default] forState:UIControlStateNormal];
                [bt setBackgroundImage:[UIImage imageNamed:KBKey_Highlight] forState:UIControlStateHighlighted];
            }
            //设置是否显示当前类型的键盘
            if (kType != KeyboardTypeNumKeyPad) {
                [bt setHidden:YES];
            }
            [self addSubview:bt];
        }
        
        //小写字母键
        for (int i=0; i<28; i++) {
            float kx ;
            float ky ;
            kx = 1 + i%7 * char_w + i%7;
            ky = char_top + i/7 * char_h + i/7;
            UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
            [bt setTag:(2000+i)];
            [bt setFrame:CGRectMake(kx, ky, char_w, char_h)];
            [bt setTitle:[charArray objectAtIndex:i] forState:UIControlStateNormal];
            
            if (i == 21) {
                [bt addTarget:self action:@selector(keyPress:) forControlEvents:UIControlEventTouchUpInside];
                
                [bt.titleLabel setFont:[UIFont boldSystemFontOfSize:Small_FontSize]];
                [bt setBackgroundImage:[UIImage imageNamed:KBKey_Highlight] forState:UIControlStateNormal];
                [bt setBackgroundImage:[UIImage imageNamed:KBKey_Default] forState:UIControlStateHighlighted];
                [bt setTitleColor:But_TextFontColor_Highlight forState:UIControlStateNormal];
                [bt setTitleColor:But_TextFontColor forState:UIControlStateHighlighted];
            } else if (i == 27){
                [bt addTarget:self action:@selector(delKeyPressBegin:) forControlEvents:UIControlEventTouchDown];  
                [bt addTarget:self action:@selector(delKeyPressEnd:) forControlEvents:UIControlEventTouchUpInside];  
                [bt addTarget:self action:@selector(delKeyPressEnd:) forControlEvents:UIControlEventTouchUpOutside];
                
                [bt.titleLabel setFont:[UIFont boldSystemFontOfSize:Small_FontSize]];
                [bt setBackgroundImage:[UIImage imageNamed:KBKey_Del_Default_1] forState:UIControlStateNormal];
                [bt setBackgroundImage:[UIImage imageNamed:KBKey_Del_Highlight_1] forState:UIControlStateHighlighted];
                [bt setTitleColor:But_TextFontColor_Highlight forState:UIControlStateNormal];
                [bt setTitleColor:But_TextFontColor forState:UIControlStateHighlighted];
            } else {
                [bt addTarget:self action:@selector(keyPress:) forControlEvents:UIControlEventTouchUpInside];
                
                [bt.titleLabel setFont:[UIFont boldSystemFontOfSize:Char_FontSize]];
                [bt setBackgroundImage:[UIImage imageNamed:KBKey_Default] forState:UIControlStateNormal];
                [bt setBackgroundImage:[UIImage imageNamed:KBKey_Highlight] forState:UIControlStateHighlighted];
                [bt setTitleColor:But_TextFontColor forState:UIControlStateNormal];
                [bt setTitleColor:But_TextFontColor_Highlight forState:UIControlStateHighlighted];
            }
            if (kType != KeyboardTypeCharKeyPad) {
                [bt setHidden:YES];
            }
            [self addSubview:bt];
        }
        
        //大写字母键
        for (int i=0; i<28; i++) {
            float kx ;
            float ky ;
            kx = 1 + i%7 * char_w + i%7;
            ky = char_top + i/7 * char_h + i/7;
            UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
            [bt setTag:(3000+i)];
            [bt setFrame:CGRectMake(kx, ky, char_w, char_h)];
            [bt setTitle:[upperCharArray objectAtIndex:i] forState:UIControlStateNormal];
            
            if (i ==21) {
                [bt addTarget:self action:@selector(keyPress:) forControlEvents:UIControlEventTouchUpInside];
                
                [bt.titleLabel setFont:[UIFont boldSystemFontOfSize:Small_FontSize]];
                [bt setBackgroundImage:[UIImage imageNamed:KBKey_Highlight] forState:UIControlStateNormal];
                [bt setBackgroundImage:[UIImage imageNamed:KBKey_Default] forState:UIControlStateHighlighted];
                [bt setTitleColor:But_TextFontColor_Highlight forState:UIControlStateNormal];
                [bt setTitleColor:But_TextFontColor forState:UIControlStateHighlighted];
            } else if (i == 27) {
                [bt addTarget:self action:@selector(delKeyPressBegin:) forControlEvents:UIControlEventTouchDown];  
                [bt addTarget:self action:@selector(delKeyPressEnd:) forControlEvents:UIControlEventTouchUpInside];  
                [bt addTarget:self action:@selector(delKeyPressEnd:) forControlEvents:UIControlEventTouchUpOutside];
                
                [bt.titleLabel setFont:[UIFont boldSystemFontOfSize:Small_FontSize]];
                [bt setBackgroundImage:[UIImage imageNamed:KBKey_Del_Default_1] forState:UIControlStateNormal];
                [bt setBackgroundImage:[UIImage imageNamed:KBKey_Del_Highlight_1] forState:UIControlStateHighlighted];
                [bt setTitleColor:But_TextFontColor_Highlight forState:UIControlStateNormal];
                [bt setTitleColor:But_TextFontColor forState:UIControlStateHighlighted];
            } else {
                [bt addTarget:self action:@selector(keyPress:) forControlEvents:UIControlEventTouchUpInside];
                
                [bt.titleLabel setFont:[UIFont boldSystemFontOfSize:Char_FontSize]];
                [bt setBackgroundImage:[UIImage imageNamed:KBKey_Default] forState:UIControlStateNormal];
                [bt setBackgroundImage:[UIImage imageNamed:KBKey_Highlight] forState:UIControlStateHighlighted];
                [bt setTitleColor:But_TextFontColor forState:UIControlStateNormal];
                [bt setTitleColor:But_TextFontColor_Highlight forState:UIControlStateHighlighted];
            }
            if (kType != KeyboardTypeUpperKeyCharPad) {
                [bt setHidden:YES];
            }
            [self addSubview:bt];
        }
        
        keyboardType = kType;
        //把当前view装进套子,就暂且叫套子吧,哈哈,也不知道改叫它啥玩意。
		[_actionSheet addSubview:self];
    }
    return self;
}

//取消按钮
-(void)CustomKeyboardViewDismiss{
    [self.delegate wdlkeyboardView:self doneWithValue:defaultValue];
	[_actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}

//确认按钮
-(void)CustomKeyboardViewSubmit{
	[self.delegate wdlkeyboardView:self doneWithValue:keyboardValue];
	[_actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}

//一般键盘
-(IBAction)keyPress:(id)sender{
    UIButton *butItem = (UIButton*)sender;
    NSString *butTitle = butItem.titleLabel.text;
    if ([butTitle isEqualToString:@"abc"] || [butTitle isEqualToString:@"ABC"] || [butTitle isEqualToString:@"123"]) {
        //更换当前键盘类型
        [self changeKeyboardType];
    } else {
        //更改当前文本值
        if (keyboardValueMaxLength > 0) {
            if ([keyboardValue length] < keyboardValueMaxLength) {
                keyboardValue = [NSString stringWithFormat:@"%@%@",keyboardValue,butTitle];
            }
        } else {
            keyboardValue = [NSString stringWithFormat:@"%@%@",keyboardValue,butTitle];
        }
        [_txtInput setText:keyboardValue];
    }
}

//开始执行删除
-(IBAction)delKeyPressBegin:(id)sender{
    //执行删除
    [self delTextValue];
    //开启Timer
	self.timer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(delTextValue) userInfo:nil repeats:YES];
}

//结束执行删除
-(IBAction)delKeyPressEnd:(id)sender{
    //停止Timer
    [self stopTimer];
}

//删除文本值
-(void)delTextValue{
    if (keyboardValue.length > 0) {
        keyboardValue = [keyboardValue substringToIndex:(keyboardValue.length-1)];
        [_txtInput setText:keyboardValue];
    }
}

//显示键盘
-(void)showKeyboardView:(NSString*)inputValue{
    defaultValue = inputValue;
    keyboardValue = inputValue;
    [_txtInput setText:inputValue];
	UIViewController *viewController = (UIViewController*)delegate;
	[_actionSheet showInView:viewController.view.window];
}

//更改键盘类型
-(void)changeKeyboardType{
    if (keyboardType == KeyboardTypeNumKeyPad) {
        for (int i=0; i<12; i++) {
            int oldButTag = (1000+i);
            UIButton *oldBut = (UIButton *)[self viewWithTag:oldButTag];
            [oldBut setHidden:YES];
        }
        for (int i=0; i<28; i++) {
            int oldButTag = (2000+i);
            UIButton *oldBut = (UIButton *)[self viewWithTag:oldButTag];
            [oldBut setHidden:NO];
        }
        keyboardType = KeyboardTypeCharKeyPad;
    } else if (keyboardType == KeyboardTypeCharKeyPad) {
        for (int i=0; i<28; i++) {
            int oldButTag = (2000+i);
            UIButton *oldBut = (UIButton *)[self viewWithTag:oldButTag];
            [oldBut setHidden:YES];
        }
        for (int i=0; i<28; i++) {
            int oldButTag = (3000+i);
            UIButton *oldBut = (UIButton *)[self viewWithTag:oldButTag];
            [oldBut setHidden:NO];
        }
        keyboardType = KeyboardTypeUpperKeyCharPad;
    } else if (keyboardType == KeyboardTypeUpperKeyCharPad) {
        for (int i=0; i<28; i++) {
            int oldButTag = (3000+i);
            UIButton *oldBut = (UIButton *)[self viewWithTag:oldButTag];
            [oldBut setHidden:YES];
        }
        for (int i=0; i<12; i++) {
            int oldButTag = (1000+i);
            UIButton *oldBut = (UIButton *)[self viewWithTag:oldButTag];
            [oldBut setHidden:NO];
        }
        keyboardType = KeyboardTypeNumKeyPad;
    }
}

@end

 

 

第四步,贴图。

 

 

第五步,上工程代码,下面有项目代码,灰常小。

 

 

 

  • 大小: 64 KB
  • 大小: 145.9 KB
  • 大小: 150.7 KB
  • 大小: 150.3 KB
  • 大小: 135.1 KB
  • 大小: 46 KB
分享到:
评论

相关推荐

    iphone 自定义键盘实例

    这个“iphone 自定义键盘实例”是一个实际的项目,可以帮助开发者了解如何创建自己的键盘应用。虽然方法可能较为基础,但它提供了一个学习自定义键盘实现的良好起点。 在iOS中,自定义键盘的开发主要涉及以下知识点...

    iphone自定义数字键盘

    确保你的自定义键盘能够适配不同的屏幕尺寸(iPhone、iPad)和设备方向(横屏、竖屏)。这可能涉及到调整键盘视图的约束或者重新布局按钮。 六、测试与优化 在实际应用中,务必进行充分的测试,确保在不同情况下...

    实现IPHONE自定义键盘

    本文将详细介绍如何在iPhone上创建并实现自定义键盘。 首先,我们要理解实现自定义键盘的基本思路。苹果提供了允许开发者访问和操作系统键盘的能力,但不是通过直接替换系统键盘,而是通过在现有键盘上叠加自定义...

    iphone自定义键盘

    标题“iPhone自定义键盘”揭示了我们今天要探讨的核心主题,即如何在iOS平台上为iPhone创建自定义的输入法键盘。这通常涉及到深入理解iOS应用开发,特别是与UIKeyboardType相关的部分,以及如何通过第三方库或者...

    iOS自定义键盘

    在iOS开发中,自定义键盘是一项常见的需求,特别是在创建具有特定输入要求的应用时。本教程将深入探讨如何使用Objective-C来实现一个简单的自定义数字键盘。我们先从基础概念开始,然后逐步构建出完整的键盘实现。 ...

    自定义全部类型键盘

    在iOS开发中,自定义键盘是一项常见的需求,尤其在创建特定功能或提升用户体验时。标题“自定义全部类型键盘”表明我们将讨论如何构建一个能够切换字母、数字和符号的全面键盘,这通常涉及到UIKeyboardType的自定义...

    自定义数字键盘,添加DONE按钮

    在实现自定义键盘的交互时,需要处理两个关键问题:一是如何使自定义键盘与文本输入视图关联,二是如何响应用户的输入。在ViewController.m文件中,你需要继承`UIInputViewController`而不是普通的UIViewController...

    自定义键盘的实现源码

    2. 自定义键盘的动画效果:如滑动切换数字和字母键盘,或者按键按下时的视觉反馈。 3. 更多的交互功能:如剪贴板操作、撤销/重做功能、表情选择等。 4. 键盘记忆功能:记住用户的输入习惯,如最近输入的数字或单词。...

    ios-自定义可变数字键盘.zip

    9. **内存管理和性能优化**: 由于键盘经常需要显示和隐藏,所以自定义键盘的内存管理很重要。此外,优化视图的渲染和更新以提高性能也是必要的。 10. **测试与调试**: 自定义键盘的测试涵盖各个方面,包括不同设备...

    ios 自定义类数字键盘

    iOS自定义数字键盘 使用简单 适配iPhone6/6PLUS self.keyBoardTF.inputView = [[WSNumberKeyBoard alloc] initWithControlTextField:self.keyBoardTF ReturnButtonName:nil andClickBlock:^(UITextField *...

    ios-完全自定义键盘.zip

    6. **适配不同设备和方向**:确保自定义键盘在iPhone和iPad上,以及横竖屏模式下都能正常工作。 7. **合规性检查**:注意遵循Apple的Human Interface Guidelines,确保自定义键盘与iOS的其他部分保持一致,提供良好...

    IOS应用源码——自定义数字键盘.rar

    - 与UITextField或UITextView的集成:自定义键盘通常与输入框配合使用,需要监听输入框的`becomeFirstResponder`和`resignFirstResponder`事件,以便在合适的时候显示和隐藏键盘。 - 键盘动画:可以添加平滑的动画...

    reactnative自定义键盘

    React Native自定义键盘是混合移动应用开发中的一个重要话题,它涉及到使用JavaScript技术在iOS和Android平台上构建原生应用。React Native是由Facebook推出的一种框架,它允许开发者使用JavaScript编写代码,同时...

    iOS设备检测及iPhone数字键盘添加完成按钮工程代码

    在iOS开发中,掌握设备检测和自定义键盘功能是至关重要的技能。本项目"iOS设备检测及iPhone数字键盘添加完成按钮工程代码"旨在提供一种实用的方法来检测iOS设备类型,并针对iPhone的数字键盘添加一个自定义的“完成...

    自定义iphone铃声

    ### 自定义iPhone铃声知识点详解 #### 一、前言 在数字时代,个性化成为越来越多人追求的目标之一。对于iPhone用户来说,自定义铃声无疑是一种展现个性的方式。本篇文章将详细介绍如何通过简单的步骤实现iPhone...

    iPhone在数字键盘上添加完成按键

    在iOS开发中,为iPhone的数字键盘添加一个“完成”按键可以极大提升用户输入体验,尤其是在需要快速输入一串数字并确认的情况下。这个功能可以通过创建自定义键盘扩展来实现,以下将详细介绍如何进行操作。 首先,...

    ios-纯数字键盘.zip

    1. **自定义键盘**: iOS系统默认提供了多种键盘类型,如默认键盘、数字与符号键盘等,但有时这些键盘并不能满足特定需求,比如在需要用户输入纯数字的场景。因此,自定义键盘应运而生。这个资源包提供的就是一个定制...

    仿iphone软键盘输入文本框.zip

    5. **自定义配置**:为了适应不同的应用场景,组件可能提供了一些自定义选项,比如键盘布局、按键内容、颜色方案等,让开发者可以根据需求进行调整。 6. **兼容性考虑**:由于CSS3和jQuery并非所有浏览器都完全支持...

    自定义的View始终位于弹起键盘顶部

    考虑到不同设备(iPhone、iPad)和屏幕方向(横屏、竖屏),键盘的大小和位置可能会有所不同,自定义视图的定位逻辑需要能够灵活适应这些变化。 9. **可复用组件**: 为了提高代码的复用性,可以封装一个类别...

Global site tag (gtag.js) - Google Analytics