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和一个enter按钮的小键盘,没有别的。键盘应该像标准键盘一样工作和表现(在行为上),但它肯定会看起来不同。
I can't find any example and the best I've found is to filter characters with existing keyboard which is an unacceptable solution.
我找不到任何例子,我找到的最好的方法就是用现有的键盘来过滤字符,这是一个无法接受的解决方案。
5 个解决方案
#1
38
I think you're looking for the "Text, Web, and Editing Programming Guide for iOS"
我认为你在寻找“文本,网络,和编辑的iOS编程指南”
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.
UIKit框架包括对自定义输入视图和输入附件视图的支持。当用户在视图中编辑文本或其他形式的数据时,应用程序可以将自己的输入视图替换为系统键盘。例如,应用程序可以使用自定义输入视图从符文字母表中输入字符。还可以将输入附件视图附加到系统键盘或自定义输入视图;这个附属视图沿着主输入视图的顶部运行,可以包含,例如,以某种方式影响文本的控件或显示有关文本的一些信息的标签。
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...
如果您的应用程序正在使用UITextView和UITextField对象进行文本编辑,那么只需将自定义视图分配给inputView和inputAccessoryView属性。当文本对象成为第一响应者时,将显示这些自定义视图。
This might serve as a good introduction: customizing the iOS keyboard
这可能是一个很好的介绍:定制iOS键盘
#3
3
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:
当点击进入该字段时,我确实滚动视图pup(如果必要)以不覆盖该字段:
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;
#4
2
You want to set the value for
您需要设置值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.
可以用来添加少量的功能。视图将被放置在系统键盘之上,并将到达并随它被驳回。
#5
2
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://*.com/a/13351686/937822
具体细节在我的另一个答案上:https://*.com/a/13351686/937822。
#1
38
I think you're looking for the "Text, Web, and Editing Programming Guide for iOS"
我认为你在寻找“文本,网络,和编辑的iOS编程指南”
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.
UIKit框架包括对自定义输入视图和输入附件视图的支持。当用户在视图中编辑文本或其他形式的数据时,应用程序可以将自己的输入视图替换为系统键盘。例如,应用程序可以使用自定义输入视图从符文字母表中输入字符。还可以将输入附件视图附加到系统键盘或自定义输入视图;这个附属视图沿着主输入视图的顶部运行,可以包含,例如,以某种方式影响文本的控件或显示有关文本的一些信息的标签。
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...
如果您的应用程序正在使用UITextView和UITextField对象进行文本编辑,那么只需将自定义视图分配给inputView和inputAccessoryView属性。当文本对象成为第一响应者时,将显示这些自定义视图。
This might serve as a good introduction: customizing the iOS keyboard
这可能是一个很好的介绍:定制iOS键盘
#2
#3
3
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:
当点击进入该字段时,我确实滚动视图pup(如果必要)以不覆盖该字段:
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;
#4
2
You want to set the value for
您需要设置值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.
可以用来添加少量的功能。视图将被放置在系统键盘之上,并将到达并随它被驳回。
#5
2
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://*.com/a/13351686/937822
具体细节在我的另一个答案上:https://*.com/a/13351686/937822。