ios 如何创建自定义键盘

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4643167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:28:55  来源:igfitidea点击:

How to create a custom keyboard

iphoneobjective-cioskeyboard

提问by HM1

How do I go about creating a custom keyboard/keypad that will show up when some one taps on a UITextField? I would like to display a keypad with a, b, c, 1, 2, 3 and an enter button, nothing else. The keypad should work and behave like the standard keyboard does (in behavior) but it will definitely look different.

如何创建自定义键盘/小键盘,当有人点击 UITextField 时会显示该键盘/小键盘?我想显示一个带有 a、b、c、1、2、3 和回车按钮的键盘,没有别的。小键盘应该像标准键盘一样工作和表现(在行为上),但它看起来肯定会有所不同。

I can't find any example and the best I've found is to filter characters with existing keyboard which is an unacceptable solution.

我找不到任何示例,我发现的最好的方法是使用现有键盘过滤字符,这是不可接受的解决方案。

回答by Jonah

I think you're looking for the "Text, Web, and Editing Programming Guide for iOS"

我认为您正在寻找“适用于 iOS 的文本、Web 和编辑编程指南

The UIKit framework includes support for custom input views and input accessory views. Your application can substitute its own input view for the system keyboard when users edit text or other forms of data in a view. For example, an application could use a custom input view to enter characters from a runic alphabet. You may also attach an input accessory view to the system keyboard or to a custom input view; this accessory view runs along the top of the main input view and can contain, for example, controls that affect the text in some way or labels that display some information about the text.

To get this feature if your application is using UITextView and UITextField objects for text editing, simply assign custom views to the inputView and inputAccessoryView properties. Those custom views are shown when the text object becomes first responder...

UIKit 框架包括对自定义输入视图和输入附件视图的支持。当用户在视图中编辑文本或其他形式的数据时,您的应用程序可以用自己的输入视图代替系统键盘。例如,应用程序可以使用自定义输入视图来输入符文字母表中的字符。您还可以将输入附件视图附加到系统键盘或自定义输入视图;这个附属视图沿着主输入视图的顶部运行,并且可以包含例如以某种方式影响文本的控件或显示有关文本的一些信息的标签。

如果您的应用程序使用 UITextView 和 UITextField 对象进行文本编辑,要获得此功能,只需将自定义视图分配给 inputView 和 inputAccessoryView 属性。当文本对象成为第一响应者时,将显示这些自定义视图...



This might serve as a good introduction: customizing the iOS keyboard

这可能是一个很好的介绍:自定义 iOS 键盘

回答by Muhammad Towfique Imam

You can use Custom-iOS-Keyboardslibrary on Github.

你可以在 Github 上使用Custom-iOS-Keyboards库。

回答by GeT

First of all create a view (I did it in a separate nib file and loaded it this way):

首先创建一个视图(我在一个单独的 nib 文件中完成并以这种方式加载它):

    NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"ReducedNumericKeyboardView"
                                                   owner:self
                                                 options:nil];
    keyBView = (ReducedNumericKeyboardView*)[views objectAtIndex:0];

After it, I set it as input view for the text field where i want to use it (and actually, this is the short answer to your question ;) ):

之后,我将其设置为我想使用它的文本字段的输入视图(实际上,这是对您问题的简短回答;)):

    [self.propertyEditor setInputView:keyBView];  

When clicking into the field i do scroll the view pup (if necessary) to not cover the field:

单击该字段时,我会滚动视图幼崽(如有必要)以不覆盖该字段:

CGRect textFieldRect = [self.tableViewController.view.window convertRect:propertyEditor.bounds fromView:propertyEditor];
CGRect viewRect = [self.tableViewController.view.window convertRect:self.tableViewController.view.bounds fromView:self.tableViewController.view];
CGFloat midLine = textFieldRect.origin.y+.5*textFieldRect.size.height;

CGFloat numerator = midLine - viewRect.origin.y - MINIMUM_SCROLL_FRACTION*viewRect.size.height;
CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)*viewRect.size.height;
CGFloat heightFraction = MIN(1, MAX(0, numerator/denominator));

animateDistance = floor(PORTRAIT_USER_INPUT_VIEW_HEIGHT*heightFraction);

CGRect viewFrame = self.tableViewController.view.frame;
viewFrame.origin.y -= animateDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:USER_INPUT_ANIMATION_DURATION];
[self.tableViewController.view setFrame:viewFrame];
[UIView commitAnimations];

When editing is finished, I do scroll the view down:

编辑完成后,我会向下滚动视图:

        CGRect viewFrame = self.tableViewController.view.frame;
        viewFrame.origin.y += animateDistance;
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationBeginsFromCurrentState:YES];
        [UIView setAnimationDuration:USER_INPUT_ANIMATION_DURATION];
        [self.tableViewController.view setFrame:viewFrame];
        [UIView commitAnimations];

The constraints I use are set as follows:

我使用的约束设置如下:

static const CGFloat USER_INPUT_ANIMATION_DURATION = 0.3;
static const CGFloat PORTRAIT_USER_INPUT_VIEW_HEIGHT = 180;

static const CGFloat MINIMUM_SCROLL_FRACTION = 0.1;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.2;

回答by lnafziger

Unfortunately, the "Text, Web, and Editing Programming Guide for iOS" referenced above doesn't give any information on what to do with the character once you press a key. This is by far the hard part when implementing a keyboard in iOS.

不幸的是,上面引用的“iOS 文本、Web 和编辑编程指南”没有提供有关按下某个键后如何处理该字符的任何信息。这是迄今为止在 iOS 中实现键盘时最困难的部分。

I have created a full working example of a hex numberpad which can easily be customized with like you need.

我创建了一个完整的十六进制数字键盘示例,可以根据需要轻松自定义。

Specific details are at my other answer on this subject: https://stackoverflow.com/a/13351686/937822

具体细节在我关于这个主题的其他答案中:https: //stackoverflow.com/a/13351686/937822

回答by bshirley

You want to set the value for

你想设置值

inputView

on your UITextField. You will need to fully implement a new keyboard or input mechanism in the view you provide.

在您的 UITextField 上。您需要在您提供的视图中完全实现新的键盘或输入机制。

Alternately,

交替,

inputAccessoryView 

can be used to add a small amount of functionality. The view will be placed above the system keyboard and will arrive and be dismissed with it.

可用于添加少量功能。视图将被放置在系统键盘上方,并且将随它到达和消失。