自定义键盘

具体的请参考附件,这里只列出核心功能的代码。

 

- (void)configKeypad {
	if ([self.addView.subviews count] >= 0) {
		for(UIView *mySubview in self.addView.subviews) {
			[mySubview removeFromSuperview];
		}
	}
	for (int i = 0; i < 5; i++) {
		for (int j = 0; j < 6; j++) {
			UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
			button.tag = i + j * 5;
            
			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];
			[button addSubview:label];
			[label release];
			
			[button setImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
			[button addTarget:self action:@selector(buttonClickAtIndex:) forControlEvents:UIControlEventTouchUpInside];
			button.frame = CGRectMake(64 * i, 2 + 35 * j, 62, 34);
			[self.addView addSubview:button];
		}
	}	
}

- (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 configKeypad];
		}
	}
	
	if (button.tag == 27) {
		isNum = !isNum;
		[self configKeypad];
		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];
	self.myTextField.text = myText;
}

 

示例图:


自定义键盘

你可能感兴趣的:(ios,iPhone,键盘)