Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Setting TouchScreenKeyboard Capitalization

Discussion in 'iOS and tvOS' started by fingersbleeding, Apr 10, 2014.

  1. fingersbleeding

    fingersbleeding

    Joined:
    Jun 10, 2013
    Posts:
    24
    In ObjectiveC / XCode, working with input fields in a StoryBoard or InterfaceBuilder, you can select the Capitalization settings. The options are None, Words, Sentences, and All Characters. These override the device's Keyboard preferences in Settings. The most common use is you can prevent accidental capitalization of the first character in username / password fields.

    Looking at the TouchScreenKeyboard.Open method, you can set basic levels on the other common params available to input fields on iOS (autocorrection, multiline), but there's no param for capitalization.

    TouchScreenKeyboard.Open(text: string, keyboardType: TouchScreenKeyboardType = TouchScreenKeyboardType.Default, autocorrection: bool = true, multiline: bool = false, secure: bool = false, alert: bool = false, textPlaceholder: string = ""):

    Are capitalization options exposed in Unity?
     
  2. fingersbleeding

    fingersbleeding

    Joined:
    Jun 10, 2013
    Posts:
    24
    Here's a temporary solution until the Unity team can properly address this issue.

    1. Publish from Unity to iOS to create an XCode project.
    2. In XCode, open Classes > UI > Keyboard.mm.
    3. Under line 134:
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    Add the following (will become line 135)
    textField.autocapitalizationType = UITextAutocapitalizationTypeNone;

    This will set all of your textfields in Unity (on iOS) to use a keyboard that defaults to lowercase without the shift key automatically pressed.

    The downside to this solution is that if you re-publish from Unity and select Replace, Unity will overwrite this Keyboard.mm file and you'll lose this edit.

    The problem is that the Unity team hasn't exposed UIKit's UITextField's autocapitalizationType as part of the "bridge" method that handles Unity's TouchScreenKeyboard.Open call. You can see in Keyboard.mm (lines 334-360) that Unity exposes UIKeyboardType, UITextAutocorrectionType, and UIKeyboardAppearance, and sets those values to a UITextField (lines 187-192) during the ObjectiveC show: method (starting on line 169).

    May we have UITextField's autocorrectionType param exposed as part of the next release of the iOS packager / TouchScreenKeyboard.Open method?
     
  3. Barrett Fox

    Barrett Fox

    Joined:
    Jul 21, 2008
    Posts:
    14
    Bump. It'd be nice if there were an easier solution.
     
  4. taxfromdk

    taxfromdk

    Joined:
    Oct 30, 2009
    Posts:
    95
    Hi,

    I had this problem

    I made a postprocessor script that does the change that fingersbleeding suggested.

    Dump it in a editorfolder. E.g:

    /Assets/Editor/KeyboardBuildPostProcessor.cs

    And it will run after you build your game.

    Use with caution, as it has effect on all keyboards invoked from the build and the code that need to be changed might change in newer Unity builds. Im on Unity 5.1 :)

    Jesper
     

    Attached Files:

  5. HakanOzcan

    HakanOzcan

    Joined:
    May 14, 2013
    Posts:
    40
    Still no option for autocapitalization? Sometimes unity makes me crazy!
    Is there anybody who tested this postprocessor script works on newer versions of Unity like 2018.3?
     
  6. Aidan-Wolf

    Aidan-Wolf

    Joined:
    Jan 6, 2014
    Posts:
    58
    @HakanOzcan just figured it out. You need to replace line 324 in Classes/UI/Keyboard.mm
    Code (CSharp):
    1. //original
    2. UITextAutocapitalizationType capitalization = UITextAutocapitalizationTypeSentences;
    3. //new
    4. UITextAutocapitalizationType capitalization = UITextAutocapitalizationTypeNone;
    You can do so with this postbuild script (I also added a step to remove that ugly done/cancel toolbar)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEditor;
    4. using UnityEditor.Callbacks;
    5.  
    6. public class KeyboardBuildPostProcessor
    7. {
    8.     [PostProcessBuild]
    9.     public static void OnPostProcessBuild(BuildTarget target, string pathToBuiltProject)
    10.     {
    11.         Debug.Log ("OnPostProcessBuild");
    12.         if(target == BuildTarget.iOS)
    13.         {
    14.             string replace = "UITextAutocapitalizationType capitalization = UITextAutocapitalizationTypeSentences";
    15.             string with = "UITextAutocapitalizationType capitalization = UITextAutocapitalizationTypeNone";
    16.            
    17.             Debug.Log (target.ToString());
    18.             Debug.Log (pathToBuiltProject);
    19.             Debug.Log ("Removing autouppercase on keyboard");
    20.             string targetfile = pathToBuiltProject + "/Classes/UI/Keyboard.mm";
    21.             string filecontents = System.IO.File.ReadAllText(targetfile);
    22.             if(filecontents.Contains(replace))
    23.             {
    24.                 Debug.Log ("replacing autocapitalization type");
    25.                 filecontents = filecontents.Replace(replace, with);
    26.             }
    27.  
    28.             if(filecontents.Contains("[self createToolbars]"))
    29.             {
    30.                 Debug.Log ("commenting out [self createToolbars]");
    31.                 filecontents = filecontents.Replace("[self createToolbars]", "//[self createToolbars]");
    32.             }
    33.            
    34.             System.IO.File.WriteAllText(targetfile, filecontents);
    35.         }
    36.     }
    37. }
    38.  
     
  7. coshea

    coshea

    Joined:
    Dec 20, 2012
    Posts:
    317
    Our project is Unity 2019, we were wondering why the text input had shift lock on by default and stumbled upon this very old thread. What's the current situation on this issue? Thanks
     
  8. mafsays

    mafsays

    Joined:
    Mar 11, 2021
    Posts:
    2
    hey @coshea :D and anyone else still banging their heads against this 9 year old issue, I got this working using the @Aidan-Wolf 's snipped but had to remove the section that comments out "[self createToolbars]" - not sure what this is or why it is there? So ended up with this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEditor;
    4. using UnityEditor.Callbacks;
    5. public class KeyboardBuildPostProcessor
    6. {
    7.     [PostProcessBuild]
    8.     public static void OnPostProcessBuild(BuildTarget target, string pathToBuiltProject)
    9.     {
    10.         Debug.Log ("OnPostProcessBuild");
    11.         if(target == BuildTarget.iOS)
    12.         {
    13.             string replace = "UITextAutocapitalizationType capitalization = UITextAutocapitalizationTypeSentences";
    14.             string with = "UITextAutocapitalizationType capitalization = UITextAutocapitalizationTypeNone";
    15.          
    16.             Debug.Log (target.ToString());
    17.             Debug.Log (pathToBuiltProject);
    18.             Debug.Log ("Removing autouppercase on keyboard");
    19.             string targetfile = pathToBuiltProject + "/Classes/UI/Keyboard.mm";
    20.             string filecontents = System.IO.File.ReadAllText(targetfile);
    21.             if(filecontents.Contains(replace))
    22.             {
    23.                 Debug.Log ("replacing autocapitalization type");
    24.                 filecontents = filecontents.Replace(replace, with);
    25.             }
    26.          
    27.             System.IO.File.WriteAllText(targetfile, filecontents);
    28.         }
    29.     }
    30. }