谁有自定义键盘的代码例子 给我参考参考
解决方案 »
- IOS获取本地应用程序信息
- asp.net如何获取 ios post过来数据
- 91安装,报错:不同格式的软件已经存在与目录中 同步助手安装:错误:-402636802
- 安装xcode3.6出错
- 【IOS开发】webapp开发者准备转IOS开发,目前小白,请教一个问题
- MPMoviePlayerController播放过程中暂停了
- 问一个关于下载文件的问题
- 请问为什么不要@synthesize ,也能生成get set方法?
- 开始学习ios,菜鸟凑个热闹。
- 直播应用 转入后台后,如何在手机界面显示顶部红色状态栏?
- 请教UITabBarController 调整UITabbar的位置
- 请问,苹果APP发布被拒之后,怎么提交演示视频
IBOutlet UITextField *myTextField;
IBOutlet UIButton *myButton;
UIView *addView;
IBOutlet UILabel *myLabel;
BOOL isNum;
BOOL isSmallLetter;
}
@property(nonatomic,retain) UITextField *myTextField;
@property(nonatomic,retain) UIButton *myButton;
@property(nonatomic,retain) UIView *addView;
@property(nonatomic,retain) UILabel *myLabel;
-(IBAction)keyboardDown:(id)sender;
-(void)congfigKeypad;
@end#import "KeyboardDesignDemoViewController.h"@implementation KeyboardDesignDemoViewController@synthesize myTextField;
@synthesize myButton;
@synthesize addView;
@synthesize myLabel;- (void)viewDidLoad {
[super viewDidLoad];
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 480, 320, 214)];
myView.backgroundColor = [UIColor whiteColor];
[self.myTextField setKeyboardAppearance:UIKeyboardAppearanceAlert];
[self.myTextField setKeyboardType:UIKeyboardTypeASCIICapable];
[self.myTextField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[self.myTextField setAutocorrectionType:UITextAutocorrectionTypeNo];
self.addView = myView;
[self congfigKeypad];
[myView release];
//监听键盘显示和隐藏的两个notification,进行相应处理
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShowOnDelay:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(showCloseKeyBoard:)
name:UIKeyboardDidShowNotification
object:nil];
}
- (void)dealloc {
[myTextField release];
[myButton release];
[addView release];
[myLabel release];
[super dealloc];
}
- (void)keyboardWillShowOnDelay:(NSNotification *)notification
{
// [self performSelector:@selector(keyboardWillShow:) withObject:nil];
}- (void)showCloseKeyBoard:(NSNotification*)notif
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:@"asdfasd" forState:UIControlStateNormal];
[btn setFrame:CGRectMake(0, 0, 80, 40)];
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
for (UIView *keyboard in tempWindow.subviews)
{
// keyboard view found; add the custom button to it
if([[keyboard description] hasPrefix:@"<UIPeripheralHostView"] == YES || [[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
{
[keyboard addSubview:btn];
break;
}
}
// NSDictionary* info = [notif userInfo];
// NSLog(@"info:%@", info);
//
// UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
// for (UIView *keyboard in tempWindow.subviews)
// {
// NSLog(@"keyboard des:%@", [keyboard description]);
//// // keyboard view found; add the custom button to it
//// if([[keyboard description] hasPrefix:@"<UIPeripheralHostView"] == YES || [[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
//// {
//// [keyboard addSubview:doneButton];
//// break;
//// }
// }
}- (void)keyboardWillShow:(NSNotification *)note
{
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];//获得键盘所在Window
[tempWindow addSubview:self.addView];//在window上增加用于显示键盘的view
//键盘显示的动画效果
[UIView beginAnimations:@"kCATransitionReveal" context:nil];
[UIView setAnimationDuration:.1f];
self.addView.frame = CGRectMake(0.0f, 265.0, 320.0f, 214.0);
[UIView commitAnimations];
}- (void)keyboardWillHide:(NSNotification *)note{
//键盘隐藏的动画效果
[UIView beginAnimations:@"kCATransitionReveal" context:nil];
[UIView setAnimationDuration:.4f];
self.addView.frame = CGRectMake(0.0f, 480.0f, 320.0f, 214.0);
[UIView commitAnimations];
}-(void)congfigKeypad{
//每次调用该方法的时候都需要清空,把之前增加的button全部都remove掉,重新创建
for(UIView *mySubview in self.addView.subviews){
[mySubview removeFromSuperview];
}
//在键盘的view上增加自定义buttons
for (int i = 0; i< 5; i++) {
for (int j = 0; j< 6; j++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag = i+j*5;//设置button的tag方法取用 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 62, 34)];
[label setTextAlignment:UITextAlignmentCenter];
if (isNum) {
if (button.tag < 9) {
label.text = [NSString stringWithFormat:@"%d",button.tag+1];
}else if (button.tag == 9) {
label.text = @"0";
}else if (button.tag>9&&button.tag<26) {
char labelTitle = '/';
labelTitle -= button.tag-10;
label.text = [NSString stringWithFormat:@"%c",labelTitle];
}else if (button.tag == 26) {
label.text = @"@";
}
}else {
if (button.tag < 26) {
char labelTitle;
if (isSmallLetter) {
labelTitle = 'a';
}else {
labelTitle = 'A';
}
labelTitle += button.tag;
label.text = [NSString stringWithFormat:@"%c",labelTitle];
}else if (button.tag == 26) {
if (isSmallLetter) {
label.text = @"大写";
}else {
label.text = @"小写";
}
}
}
if (button.tag == 27) {
label.text = @"切换";
}else if (button.tag == 28) {
label.text = @"删除";
}else if (button.tag == 29){
label.text = @"确定";
}
label.backgroundColor = [UIColor clearColor];//清除label的背景色
[button addSubview:label];//加入到button里
[label release];
[button setImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];//设置button背景
[button addTarget:self action:@selector(buttonClickAtIndex:) forControlEvents:UIControlEventTouchUpInside];//设置响应事件
button.frame = CGRectMake(0+64*i, 2+35*j, 62, 34);//根据i,j值为button布局
[self.addView addSubview:button];//加入到键盘view里
}
}
}- (void)buttonClickAtIndex:(id)sender
{
UIButton *button = (UIButton*)sender;
NSString *str = @"";
//如果是数字键盘
if (isNum) {
if (button.tag < 9) {
str = [NSString stringWithFormat:@"%d",button.tag+1];
}else if (button.tag == 9) {
str = @"0";
}else if (button.tag>9&&button.tag<26) {
char labelTitle = '/';
labelTitle -= button.tag-10;
str = [NSString stringWithFormat:@"%c",labelTitle];
}else if (button.tag == 26) {
str = @"@";
}
}else {//如果是字母键盘
if (button.tag < 26) {
char labelTitle;
if (isSmallLetter) {
labelTitle = 'a';
}else {
labelTitle = 'A';
}
labelTitle += button.tag;
str = [NSString stringWithFormat:@"%c",labelTitle];
}else if (button.tag == 26) {
isSmallLetter = !isSmallLetter;
[self congfigKeypad];
}
}
//几个控制键盘需要单独配置
if (button.tag == 27) {
isNum = !isNum;
[self congfigKeypad];
return;
}else if (button.tag == 28) {
if ([@"" isEqualToString:self.myTextField.text] ) {
return;
}
NSMutableString *myText = [NSMutableString stringWithFormat:@"%@",self.myTextField.text];
NSRange range = {([myText length]-1),1};
[myText deleteCharactersInRange:range];
self.myTextField.text = myText;
return;
}else if (button.tag == 29){
if ([@"" isEqualToString:self.myTextField.text] ) {
return;
}
myLabel.text = self.myTextField.text;
self.myTextField.text = @"";
return;
}
NSMutableString *myText = [NSMutableString stringWithFormat:@"%@",self.myTextField.text];
[myText appendFormat:@"%@",str];
//设置textfiled 的内容
self.myTextField.text = myText;}//在view上放置了一个full screen的button,这样模拟了点击view退出键盘的效果
-(IBAction)keyboardDown:(id)sender{
[self.myTextField resignFirstResponder];
}