兔子的技术博客

兔子

   :: 首页 :: 联系 :: 聚合  :: 管理
  202 Posts :: 0 Stories :: 43 Comments :: 0 Trackbacks

留言簿(10)

最新评论

阅读排行榜

评论排行榜

有一种思路叫寄生...

我相信是懒人推动了世界的发展,既然iphone有了自己的软件盘,我们什么还要自己实现其功能呢。
so,只要寄生在上面就行了。

感谢alan转载的文章给的灵感。
http://www.cocoachina.com/bbs/read.php?tid-3999.html

思路:
1.用静态方法找到应用程序当前view(window)中的UIKeyboard的view
2.在键盘的view上帖上自己的view,(精彩了,这个自己的view就是你自己键盘,任意发挥,什么类型键盘都可以做了)
3.根据需要调整系统键盘的大小以满足你想要的尺寸
4.给自己的键盘view上的button添加方法,实现功能

主要代码:
添加自身类为键盘事件的观察者
复制代码
  1. [[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector:@selector(keyboardWillShow:) 
                                                     name:UIKeyboardWillShowNotification 
                                                   object:nil];


核心思路代码:
复制代码

  1. - (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];
                
            }
        }
    }


比如配置方法可以是这样:
复制代码
  1. -(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对应的字串。
复制代码

  1. - (void)buttonClickAtIndex:(id)sender
    {
        SearBtn *btnItem = (SearBtn*)sender;
        NSString *str = btnItem->btnText;
        [s_text appendString:str];
        [sBar setText:s_text];
    }
;

再实现一个deleteChar的方法作为退格键
思路:
复制代码

  1. if ([s_text length] > 0)
        {
            NSRange rang;
            rang.location = [s_text length] - 1;
            rang.length = 1;
            [s_text deleteCharactersInRange:rang];
        }


现在点击各种文本域,应该就可以现实自己的键盘了。

继续优化
用textfield的代理方法控制键盘的字串类型,长度,和响应消失
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);

转自:http://www.cocoachina.com/bbs/simple/?t12429.html
posted on 2011-10-26 22:06 会飞的兔子 阅读(751) 评论(0)  编辑 收藏 引用 所属分类: 苹果相关

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理