- Home /
Use specific TouchScreenKeyboardType for different text fields
I have 2 text fields for input on an iOS app, one for name and one for email. I want to use TouchScreenKeyboardType.NamePhonePad and TouchScreenKeyboardType.EmailAddress for the corresponding fields. I have seen the docs at TouchScreenKeyboard.Open
public string nameText;
public string emailText;
// make name text field
nameText = GUI.TextField(new Rect(200, 300, 400, 55), nameText, 60, nameGS);
// make email field
emailText = GUI.TextField(new Rect(200, 375, 400, 55), emailText, 80, nameGS);
I'm sure many people have done this but I can't seem to put it together. Thanks for any help
Answer by Sundar · Oct 10, 2012 at 12:18 AM
Try this
public string nameText;
public string emailText;
TouchScreenKeyboard keyboardName;
TouchScreenKeyboard keyboardEmail;
// make name text field
keyboardName = TouchScreenKeyboard.Open(nameText, TouchScreenKeyboardType.Default);
nameText = GUI.TextField(new Rect(200, 300, 400, 55), nameText, 60, nameGS);
// make email field
keyboardEmail = TouchScreenKeyboard.Open(emailText, TouchScreenKeyboardType.EmailAddress);
emailText = GUI.TextField(new Rect(200, 375, 400, 55), emailText, 80, nameGS);
Thanks for the reply. Tried it. Your example shows how to set up the keyboard and its options, but I still need a way to associate a keyboard type with a certain text field. I've seen the documentation, the only examples use a button with the if(GUI.Button) but I'm not using buttons. So how are you supposed to assign a keyboard type to a textfield?
Have you tried this
nameText = keyboardName.text;
emailText = keyboardEmail.text
I tried this too, and I end up with the GUI controls in the scene not responding to touches.
I've tried this. It results in all GUI elements not responding. I'm also trying to shift focus from one text field to another.
TouchScreen$$anonymous$$eyboard name$$anonymous$$eyboard; TouchScreen$$anonymous$$eyboard email$$anonymous$$eyboard;
void OnGUI(){
name$$anonymous$$eyboard = TouchScreen$$anonymous$$eyboard.Open(nameText, TouchScreen$$anonymous$$eyboardType.NamePhonePad, false, false, false, false,"Your name (required)"); GUI.SetNextControlName("NameField"); nameText = GUI.TextField(new Rect(200, 265, 400, 55), nameText, nameGS); nameText = name$$anonymous$$eyboard.text;
email$$anonymous$$eyboard = TouchScreen$$anonymous$$eyboard.Open(emailText, TouchScreen$$anonymous$$eyboardType.EmailAddress, false, false, false, false,"Your email (required)"); GUI.SetNextControlName("EmailField"); emailText = GUI.TextField(new Rect(200, 370, 400, 55), emailText, nameGS); emailText = email$$anonymous$$eyboard.text;
Answer by cfdevdan · Nov 08, 2012 at 08:22 PM
In case anyone else is dealing with this, I ended up writing my own plugin to create native iOS textfields and textViews. It was a little tricky but not too bad. Writing a plugin basically involves creating a class in xcode, making some class methods that do what you need and then creating C methods that call the Obj-C class methods, then calling the C method in a Unity class using the method Unity shows in their docs. Building iOS Plugins
I started out by extending a plugin that I purchased and then just took what I learned from that and made my own. Here's a snippet:
+ (void)createNameTextField:(CGFloat*)xValue yValue:(CGFloat*)yValue width:(CGFloat*)width height:(CGFloat*)height placeholder:(NSString*)placeholder returnKey:(NSString*)returnKey autoCorr:(NSString*)autoCorr font:(NSString*)font fontSize:(CGFloat*)fontSize
{
CGRect frame = CGRectMake(*xValue, *yValue, *width, *height);
nameField = [[UITextField alloc] initWithFrame:frame];
nameField.keyboardType = UIKeyboardTypeNamePhonePad;
nameField.placeholder = placeholder;
[nameField setFont:[UIFont fontWithName:font size:*fontSize]];
nameField.delegate = self;
nameField.tag = 1;
// setting return key type
if ([returnKey isEqualToString:@"default"]) {
nameField.returnKeyType = UIReturnKeyDefault;
}
else if ([returnKey isEqualToString:@"next"]) {
nameField.returnKeyType = UIReturnKeyNext;
}
else if ([returnKey isEqualToString:@"done"]) {
nameField.returnKeyType = UIReturnKeyDone;
}
else {
NSLog(@"Error: choose a valid return key option (default, next, or done)");
}
// setting auto correct type
if ([autoCorr isEqualToString:@"yes"]) {
nameField.autocorrectionType = UITextAutocorrectionTypeYes;
}
else if ([autoCorr isEqualToString:@"no"]){
nameField.autocorrectionType = UITextAutocorrectionTypeNo;
}
else if ([autoCorr isEqualToString:@"default"]){
nameField.autocorrectionType = UITextAutocorrectionTypeDefault;
}
else {
NSLog(@"Error: choose a valid autocorrection option (yes, no, or default)");
}
NSLog(@"returnKey: %@", returnKey);
NSLog(@"autoCorr: %@", autoCorr);
[UnityGetGLViewController().view addSubview:nameField];
}
Then the C method
`extern "C" {
void _catchfireCreateNameTextField( float xValue, float yValue, float width, float height, const char * placeholder, const char * returnKey, const char * autoCorr, const char * font, float fontSize)
{
// [[EtceteraManager sharedManager] createNameTextField:GetStringParam(placeholder)];
[CFTextFields createNameTextField:&xValue yValue:&yValue width:&width height:&height placeholder:GetStringParam(placeholder) returnKey:GetStringParam(returnKey) autoCorr:GetStringParam(autoCorr) font:GetStringParam(font) fontSize:&fontSize];
}`
Do you have a complete working example of this source code? :)
How to call this method from Unity. In the given sample code UITextField is created in Objective-C, how to get the control of it in Unity for Events say like when user clicks "Next" or "Return" to get the action results in Unity and also to launch and dismiss the keyboard.
Answer by Karthikbs · Jun 04, 2020 at 09:26 AM
Here is the solution for still those who are looking for these options in keyboard(IOS/Android) https://github.com/mopsicus/UnityMobileInput
Your answer
![](https://koobas.hobune.stream/wayback/20220613082112im_/https://answers.unity.com/themes/thub/images/avi.jpg)