您的位置:首页 > 移动开发 > IOS开发

ios中修改数字键盘

2013-07-19 11:42 148 查看
自定义文本框:

#import <UIKit/UIKit.h>

//自定义键盘的键定义
@interface DIYKey : NSObject
{
}
@property(copy, nonatomic) NSString* name;
@property(copy, nonatomic) NSString* representedString;
@property(copy, nonatomic) NSString* displayString;
@property(copy, nonatomic) NSString* displayType;
@property(copy, nonatomic) NSString* interactionType;
@property(copy, nonatomic) NSString* variantType;
@property(assign, nonatomic) BOOL visible;
@property(assign, nonatomic) unsigned displayTypeHint;
@property(retain, nonatomic) NSString* displayRowHint;
@property(copy, nonatomic) NSArray* variantKeys;
@property(copy, nonatomic) NSString* overrideDisplayString;
@property(assign, nonatomic) BOOL disabled;
@property(assign, nonatomic) BOOL hidden;
@end

////自定义键盘的视图
@interface DIYKeyView : UIView
{
}
@property(readonly, assign, nonatomic) DIYKey* key;
@end

@protocol MJTextFieldDelegate;

@interface MJTextField : UITextField

@property(nonatomic,assign)id<MJTextFieldDelegate>  MjDelegate;
@property(nonatomic,retain) UIButton* sureButton;
-(DIYKeyView *)FindView:(NSString *)name;
-(void )addCustormButton:(NSString *)name title:(NSString *)title target:(id)target action:(SEL)action;
-(void)delCustormButton;
-(void )modifyKeyView:(NSString *)name display:(NSString *)display represent:(NSString *)represent interaction:(NSString *)type;
@end

@protocol MJTextFieldDelegate <NSObject>
-(void)keyBoardShow:(MJTextField *)textField;
-(void)keyboardHide:(MJTextField *)textField;

@end


#import "MJTextField.h"

@implementation MJTextField

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}

-(BOOL)resignFirstResponder{
BOOL ret=[super resignFirstResponder];
if(self.MjDelegate){
[self.MjDelegate keyboardHide:self];
}

return ret;
}

-(BOOL)becomeFirstResponder{
BOOL ret=[super becomeFirstResponder];
if(self.MjDelegate){
[self.MjDelegate keyBoardShow:self];
}
return ret;
}

#pragma mark -find view

-(DIYKeyView *)FindKeyView:(NSString *)name inView:(UIView *)view{
for (DIYKeyView *subview in view.subviews)
{

NSString *className = NSStringFromClass([subview class]);

if ([className isEqualToString:@"UIKBKeyView"])
{
NSLog(@"name-->%@",subview.key.name);
if((name==nil)||[subview.key.name isEqualToString:name])
return subview;

}
else
{
DIYKeyView *subview2 = [self FindKeyView:name inView:subview];
if (subview2!=nil) {
return subview2;
}

}
}
return nil;

}

-(DIYKeyView *)FindView:(NSString *)name{
UIWindow* window = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
return [self FindKeyView:name inView:window];

}

//添加键
-(void )addCustormButton:(NSString *)name title:(NSString *)title target:(id)target action:(SEL)action{
//先找到指定的键盘
DIYKeyView *view=[self FindView:name];
if(view){
self.sureButton=[[UIButton alloc] initWithFrame:view.frame];
self.sureButton.titleLabel.font=[UIFont boldSystemFontOfSize:17];
[self.sureButton setTitle:title forState:UIControlStateNormal];
[self.sureButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
[self.sureButton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[self.sureButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
self.sureButton.showsTouchWhenHighlighted=YES;
[view.superview addSubview:self.sureButton];
}

}

//删除键
-(void)delCustormButton{
if(self.sureButton){
[self.sureButton removeFromSuperview];
self.sureButton=nil;
}
}

//修改键盘
-(void )modifyKeyView:(NSString *)name display:(NSString *)display represent:(NSString *)represent interaction:(NSString *)type{
DIYKeyView *view=[self FindView:name];
if(view){
view.key.displayString=display;//键盘显示的内容
view.key.representedString=represent;//点击键盘,输入的内容
if(type){
view.key.displayType=type;
}
[view setNeedsDisplay];
}
}

@end


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

@interface ViewController : UIViewController<MJTextFieldDelegate>
- (IBAction)click:(id)sender;
@property (retain, nonatomic) IBOutlet UITextField *text;

@end

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

#pragma mark -生命周期方法
- (void)viewDidLoad
{
[super viewDidLoad];
MJTextField *mj=[[MJTextField alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
mj.borderStyle=UITextBorderStyleRoundedRect;
mj.keyboardType=UIKeyboardTypeNumberPad;
mj.tag=1;
mj.MjDelegate=self;
[self.view addSubview:mj];
[mj release];

MJTextField *mj1=[[MJTextField alloc] initWithFrame:CGRectMake(0, 150, 100, 50)];
mj1.borderStyle=UITextBorderStyleRoundedRect;
mj1.keyboardType=UIKeyboardTypeDefault;
mj1.MjDelegate=self;
mj1.tag=2;
mj1.returnKeyType=UIReturnKeyDone;
[self.view addSubview:mj1];
[mj1 release];

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

-(void)keyBoardShow:(MJTextField *)textField{

// Let's crazy!
if(1==textField.tag){
[textField modifyKeyView:@"NumberPad-1" display:@"x" represent:@"aa" interaction:nil];

}

}
-(void)keyboardHide:(MJTextField *)textField{
[textField delCustormButton];
}

- (void)dealloc
{

[super dealloc];
}

-(void)myclick:(id)sender{
NSLog(@"aa");
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: