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
相关文章推荐
- iOS swift 自定制控件(数字键盘)
- iOS键盘数字判断
- XZ_iOS之实现textField限制只能输入数字,无论是第三方键盘还是自带键盘
- iOS之 利用通知(NSNotificationCenter)获取键盘的高度,以及显示和隐藏键盘时修改界面的注意事项
- iOS数字键盘自定义按键
- ios中如何自定义数字键盘
- (收藏)在 iPhone/iPad 中随意修改数字键盘按钮
- iOS 键盘输入限制(只能输入字母,数字,禁止输入特殊符号)
- win7开机自动打开数字小键盘的修改.
- IOS 改变键盘颜色的实现---只有Number Pad和Phone Pad这两种数字键盘才有效果
- 【iOS知识学习】_iPhone学习基于UIKeyboardTypeDecimalPad(数字键盘+小数点)
- IOS修改键盘颜色
- 用Xcode6进行ios开发中,修改模拟器键盘显示中文
- ios 键盘 之 数字键盘
- 在 iPhone/iPad 中随意修改数字键盘按钮
- iOS 键盘限制只能数字和字母
- iOS 数字输入键盘添加一个完成按钮
- ios隐藏数字键盘 --在数字键盘左下角添加“完成”按钮的实现原理
- iOS 数字键盘限制输入的位数以及小数点的位数
- iOS UILabel 只修改字符串中的数字的颜色