自定义文本框:

#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:];
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:];
[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(, , , )];
mj.borderStyle=UITextBorderStyleRoundedRect;
mj.keyboardType=UIKeyboardTypeNumberPad;
mj.tag=;
mj.MjDelegate=self;
[self.view addSubview:mj];
[mj release]; MJTextField *mj1=[[MJTextField alloc] initWithFrame:CGRectMake(, , , )];
mj1.borderStyle=UITextBorderStyleRoundedRect;
mj1.keyboardType=UIKeyboardTypeDefault;
mj1.MjDelegate=self;
mj1.tag=;
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(==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

最新文章

  1. [diango]理解django视图工作原理
  2. IL指令大全(转)
  3. StoryBoard和代码结合 按比例快速兼容iPhone6/6 Plus教程
  4. 循序渐进Python3(一)-- 初识Python
  5. STM32 程序所占用空间计算 &amp;&amp; FLASH存储的起始地址计算
  6. poj 1176 Party Lamps
  7. js修改input的type属性问题
  8. Linux下Bash运行脚本
  9. oracle lag与lead分析函数简介
  10. eclipse 中 Servlet 模板代码(其实是代码提示模板)
  11. 退役之战- SDOI
  12. How hacker do IT: Tricks Tools and Techniques (翻译)
  13. 经典面试题|讲一讲JVM的组成
  14. imp、exp命令导出优化
  15. ansible 问题
  16. HDU 1879 继续畅通工程(最小生成树)
  17. 直方图与bin
  18. 【CF900D】Unusual Sequences 容斥(莫比乌斯反演)
  19. 十八、curator recipes之DistributedDelayQueue
  20. Jmeter-Interleave Controller(交替控制器)

热门文章

  1. 在EntityFramework6中执行SQL语句【转】
  2. Solr搜索结果说明 (转)
  3. Triangle leetcode java
  4. Oracle数据库将varchar类型的字段改为clob类型
  5. 转:TensorFlow入门(六) 双端 LSTM 实现序列标注(分词)
  6. RAMPS1.4 3d打印控制板接线与测试2
  7. Mybatis源码分析之SqlSessionFactory(一)
  8. Spring开发 - 通过实现ApplicationContextAware在Servlet中调用注解的Service
  9. Invalid volume failure config value: 1
  10. npm - 部分常用命令(笔记)