定制 iPhone 键盘

发布于 2024-08-08 14:08:56 字数 200 浏览 6 评论 0原文

我需要(即客户要求)提供自定义键盘,供用户在文本字段和区域中输入文本。我已经有一些可以执行键盘操作并将测试附加到文本字段的东西,但是我想让它更加通用,并让它像标准的 iPhone 键盘一样工作,即在用户选择可编辑文本控件时出现。目前我的控制器知道目标,并且目标不可编辑以防止标准键盘。

有没有办法挂钩文本控件的行为,以便我轻松使用自己的键盘?

谢谢,维克

I need (i.e. a customer requirement) to provide a custom keyboard for the user to type text into both text fields and areas. I already have something that does the keyboard and appends test to a text field, however I'd like to make it more generic and have it act like the standard iphone keyboard, i.e. appear when teh user selects an editable text control. Currently my controller knows the target and the target is uneditable to prevent the standard keyboard.

Is there a way to hook into the behavior of text controls so I use my own keyboard easily?

Thanks, Vic

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

故事未完 2024-08-15 14:08:56

这里有一个想法:根据自己的需要修改现有的键盘。首先,注册以便在它出现在屏幕上时收到通知:

[[NSNotificationCenter defaultCenter] addObserver:self 
                                      selector:@selector(modifyKeyboard:)
                                      name:UIKeyboardWillShowNotification
                                      object:nil];

然后,在您的 modifyKeyboard 方法中:

- (void)modifyKeyboard:(NSNotification *)notification 
{
    UIView *firstResponder = [[[UIApplication sharedApplication] keyWindow] performSelector:@selector(firstResponder)];

    for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows])
        for (UIView *keyboard in [keyboardWindow subviews])
            if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
            {
                MyFancyKeyboardView *customKeyboard = [[MyFancyKeyboardView alloc] initWithFrame: CGRectMake(0, 0, keyboard.frame.size.width, keyboard.frame.size.height);
                [keyboard addSubview: customKeyboard];
                [customKeyboard release];
            }
}

这会将您的视图添加到原始键盘的顶部,因此请确保将其设置为不透明。

Here's an idea: modify the existing keyboard to your own needs. First, register to be notified when it appears on screen:

[[NSNotificationCenter defaultCenter] addObserver:self 
                                      selector:@selector(modifyKeyboard:)
                                      name:UIKeyboardWillShowNotification
                                      object:nil];

Then, in your modifyKeyboard method:

- (void)modifyKeyboard:(NSNotification *)notification 
{
    UIView *firstResponder = [[[UIApplication sharedApplication] keyWindow] performSelector:@selector(firstResponder)];

    for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows])
        for (UIView *keyboard in [keyboardWindow subviews])
            if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
            {
                MyFancyKeyboardView *customKeyboard = [[MyFancyKeyboardView alloc] initWithFrame: CGRectMake(0, 0, keyboard.frame.size.width, keyboard.frame.size.height);
                [keyboard addSubview: customKeyboard];
                [customKeyboard release];
            }
}

This adds your view on top of the original keyboard, so make sure you make it opaque.

怪异←思 2024-08-15 14:08:56

您可以使用 iOS 3.2 或更高版本来完成此操作。检查 UITextField 的 inputView 属性以获取详细信息。

You can do it with iOS 3.2 or later. Check the inputView property of UITextField for details.

花伊自在美 2024-08-15 14:08:56

只要您不将应用程序提交到应用程序商店,您就可以使用一种称为方法调配的技术在运行时动态替换核心类的方法。例如:

@interface UIControl(CustomKeyboard)
- (BOOL)__my__becomeFirstResponder
@end

@implementation UIControl(CustomKeyboard)
- (BOOL)__my__becomeFirstResponder
{
    BOOL becameFirstResponder = [self __my__becomeFirstResponder];
    if ([self canBecomeFirstResponder]) {
        [MyKeyboard orderFront];
    }
    return becameFirstResponder;
}

+ (void)initialize
{
    Method old = class_getInstanceMethod(self, @selector(becomeFirstResponder));
    Method new = class_getInstanceMethod(self, @selector(__my__becomeFirstResponder));
    method_exchangeImplementations(old, new);
}
@end

不要在任何生产代码中使用类似的内容。另外,我还没有实际测试过这个,所以 YMMV。

As long as you're not submitting your app to the app store, you can use a technique called method swizzling to dynamically replace methods of core classes at runtime. For example:

@interface UIControl(CustomKeyboard)
- (BOOL)__my__becomeFirstResponder
@end

@implementation UIControl(CustomKeyboard)
- (BOOL)__my__becomeFirstResponder
{
    BOOL becameFirstResponder = [self __my__becomeFirstResponder];
    if ([self canBecomeFirstResponder]) {
        [MyKeyboard orderFront];
    }
    return becameFirstResponder;
}

+ (void)initialize
{
    Method old = class_getInstanceMethod(self, @selector(becomeFirstResponder));
    Method new = class_getInstanceMethod(self, @selector(__my__becomeFirstResponder));
    method_exchangeImplementations(old, new);
}
@end

Please don't use anything like this in any production code. Also, I haven't actually tested this, so YMMV.

小巷里的女流氓 2024-08-15 14:08:56

这取决于您想要如何定制。我个人想要一个纯数字键盘,但不喜欢数字键盘,所以我使用标准键盘并添加了标准过滤器,这是选项的一部分,它拒绝所有非数字或空格的按键或删除。

我不能说苹果是否会喜欢这个,而且你将很难编写自己的行为,就像其他键盘一样。如此之多,以至于它应该被宣布为一个坏主意。

根据您的评论更新,听起来更像是您只需要创建一个带有很多按钮的视图,然后在打开动画选项的情况下移动该视图。然后它可以像键盘一样从底部向上滑动,并在关闭时再次滑开。

It depends how custom you want it. I personally wanted a numeric only keyboard, but didn't like the number pad, so I used the standard keyboard and added in a standard filter, which is part of the options, which rejects all the key presses that aren't numbers or space or delete.

I can't say whether Apple would like this though, and you're going to have a very hard time writing your own behavior that acts like the other keyboards. So much so, that it should be declared a bad idea.

update based on your comment it sounds more like you just need to create a view with a lot of buttons on it, and move this view around with the animate option turned on. It could then sort of slide up from the bottom like a keyboard and slide away again when dismissed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文