有一種思路叫寄生...
我相信是懶人推動了世界的發(fā)展,既然iphone有了自己的軟件盤,我們什么還要自己實現(xiàn)其功能呢。
so,只要寄生在上面就行了。
感謝alan轉載的文章給的靈感。
http://www.cocoachina.com/bbs/read.php?tid-3999.html
思路:
1.用靜態(tài)方法找到應用程序當前view(window)中的UIKeyboard的view
2.在鍵盤的view上帖上自己的view,(精彩了,這個自己的view就是你自己鍵盤,任意發(fā)揮,什么類型鍵盤都可以做了)
3.根據(jù)需要調整系統(tǒng)鍵盤的大小以滿足你想要的尺寸
4.給自己的鍵盤view上的button添加方法,實現(xiàn)功能
主要代碼:
添加自身類為鍵盤事件的觀察者
復制代碼
- [[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
|
核心思路代碼:
復制代碼
- (void)keyboardWillShow:(NSNotification *)note
{
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];//知識點
for(int i=0; i<[tempWindow.subviews count]; i++)
{
keyboard = [tempWindow.subviews objectAtIndex:i];
if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
{
[keyboard setFrame:CGRectMake(0, 460, 320, 345)];
[self congfigKeypad];
[keyboard addSubview:keyPadView1];
}
}
}
|
比如配置方法可以是這樣:
復制代碼
- -(void)congfigKeypad
{
SearBtn *one = [[SearBtn alloc] initWithFrame:CGRectMake(81, 3, kNumPadW, kNumPadH) index:1 ContextString:@"1" type:kNumPadType];
[one setImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
[one addTarget:self action:@selector(buttonClickAtIndex:) forControlEvents:UIControlEventTouchUpInside];
//......略
}
|
添加NSMutalbeString作為文本域字串的容器,點擊button后append的button對應的字串。
復制代碼
- (void)buttonClickAtIndex:(id)sender
{
SearBtn *btnItem = (SearBtn*)sender;
NSString *str = btnItem->btnText;
[s_text appendString:str];
[sBar setText:s_text];
}
|
;
再實現(xiàn)一個deleteChar的方法作為退格鍵
思路:
復制代碼
if ([s_text length] > 0)
{
NSRange rang;
rang.location = [s_text length] - 1;
rang.length = 1;
[s_text deleteCharactersInRange:rang];
}
|
現(xiàn)在點擊各種文本域,應該就可以現(xiàn)實自己的鍵盤了。
繼續(xù)優(yōu)化
用textfield的代理方法控制鍵盤的字串類型,長度,和響應消失
@import url(http://www.shnenglu.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
轉自:http://www.cocoachina.com/bbs/simple/?t12429.html