直接上代码:
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 300, 40)]
//borderStyle属性用来控制输入框的外观,一共有四种:无样式(默认),线框,刀角,圆角矩形。
textField.borderStyle = UITextBorderStyleRoundedRect
//placeholder是 占位字符串,用于提示用户该输入框可以输入什么内容。
textField.placeholder = @"请输入账号"
//为文本框 添加编辑的内容
// textField.text = @"12314564654"
//开始编辑时 清空已有内容
textField.clearsOnBeginEditing = YES
//对输入的内容以 密码 形式显示 “*”,默认的为 NO 。
// textField.secureTextEntry = YES
//通过设置清除按钮的模式来控制右侧的清除按钮的显示,默认不显示,特别注意:unlessEditing表示除了编辑状态外。
textField.clearButtonMode = UITextFieldViewModeWhileEditing
//设置键盘的类型,常用的URL,EmailAddress, NumberPad, Default。
textField.keyboardType = UIKeyboardAppearanceDefault
//设置键盘样式,两种样式,浅色和深色,默认为浅色。
textField.keyboardAppearance = UIKeyboardAppearanceDark
UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.window.bounds.size.width, 40)]
aView.backgroundColor = [UIColor grayColor]
// aView.text = @"我的键盘"
// aView.textAlignment = NSTextAlignmentCenter
//为输入框添加输入辅助视图
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeSystem]
doneButton.frame = CGRectMake(self.window.bounds.size.width - 50, 0, 50, 40)
[doneButton setTitle:@"完成" forState:UIControlStateNormal]
doneButton.backgroundColor = [UIColor lightGrayColor]
[doneButton addTarget:self action:@selector(handleDoneButtonAction:) forControlEvents:UIControlEventTouchDown]
[aView addSubview:doneButton]
textField.inputAccessoryView = aView
[aView release]
UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.window.bounds.size.width, 220)]
inputView.backgroundColor = [UIColor lightGrayColor]
textField.inputView = inputView
[inputView release]
//自定义 键盘
NSArray *butArray = @[@[@"1",@"2",@"3"],@[@"4",@"5",@"6"],@[@"7",@"8",@"9"],@[@"__",@"0",@"<-"]]
for (int i = 0
for (int j = 0
UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom]
but.frame = CGRectMake(inputView.bounds.size.width / 3 * j + 1, inputView.bounds.size.height / 4 * i + 1, inputView.bounds.size.width / 3 - 2, inputView.bounds.size.height / 4 - 2)
but.backgroundColor = [UIColor grayColor]
but.showsTouchWhenHighlighted = YES
[but setTitle:butArray[i][j] forState:UIControlStateNormal]
[but setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]
[but addTarget:self action:@selector(butClickEvent:) forControlEvents:UIControlEventTouchDown]
[inputView addSubview:but]
}
}
//设置系统键盘右下角返回键的类型。
textField.returnKeyType = UIReturnKeyDone
textField.tag = 100
textField.center = self.window.center
//为textField指定代理对象。
textField.delegate = self
[self.window addSubview:textField]
[textField release]
- (void)butClickEvent:(UIButton *)sender {
// [sender titleLabel]
UITextField * temp = (UITextField *)[self.window viewWithTag:100]
temp.text = [temp.text stringByAppendingString:sender.titleLabel.text]
}