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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

InputField caret position changes with font size (4.6.4p3)

Discussion in 'UGUI & TextMesh Pro' started by DWilliams, Apr 28, 2015.

  1. DWilliams

    DWilliams

    Joined:
    Jan 12, 2015
    Posts:
    63
    Using Unity 4.6.4p3, all my input fields have incorrect caret vertical position now.

    The problem seems to be that as the font size of the text component increases the vertical alignment of the caret (and selected text highlighting) gets more and more off center and is pushed off the bottom of the input field.

    Is anyone experiencing this same issue? Is this a bug? Any help would be appreciated.
     
  2. iivo_k

    iivo_k

    Joined:
    Jan 28, 2013
    Posts:
    314
    Yeah, noticed the same thing in 4.6.4p4. Highlighting text is also misaligned.
     
  3. iivo_k

    iivo_k

    Joined:
    Jan 28, 2013
    Posts:
    314
    I reported a bug (693093). This doesn't happen in 5.0.1p2 or 5.1.0b4 though and 4.6 bugs don't seem to be a high priority.
     
  4. DWilliams

    DWilliams

    Joined:
    Jan 12, 2015
    Posts:
    63
    Bumping for any new info on this bug. Still an issue in 4.6 versions after 4.6.4p3
     
  5. michael-bartnett

    michael-bartnett

    Joined:
    Jul 8, 2012
    Posts:
    8
    Same thing happening on 5.1.1f1 (also tried 5.1.1p1, didn't help).

    It's especially rough with multiline.

    Screen Shot 2015-06-24 at 7.35.01 PM.png

    Screen Shot 2015-06-24 at 7.40.47 PM.png
     
    Last edited: Jun 25, 2015
  6. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,184
    Does anyone have a working fix for this? If I could, I'd manually hack in the numbers to align the caret correctly, but it seems that this only works, when called after selection.

    I've been trying to set the correct position in OnSelect, but then it only works the second time my field becomes selected. The first time is still ignored. Pretty annoying.
     
  7. DWilliams

    DWilliams

    Joined:
    Jan 12, 2015
    Posts:
    63
    The workaround I'm using for now is to fix the alignment like you mention, but I do it each update or lateupdate but also make sure that the script that does it is set to run after all others in the script execution order.
     
  8. Macrum

    Macrum

    Joined:
    Jul 12, 2015
    Posts:
    2
    I made this fix using a custom formula.

    caret.posY = caret.posY + (('Font.Size' - 'Working.Font.Size') * 'Scale.Ratio')

    Terms:

    Working Font Size: The font number where the caret is always on target.

    Scale Ratio: Set value to change caret position relative to the font size.

    Arial Default Values: Workning.Font.Size = 14, Scale.Ratio = 0.84

    Hope this helps.

    here is my code:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5.  
    6. //  Purpose: Reposition the "caret" when using "InputField"
    7. //  Use: Add as component to each "Input Field" object
    8. //  Made for Unity Version 5.1.1
    9. public class CaretManage : MonoBehaviour {
    10.  
    11.     //Controls access to the subset of 'IF' statements
    12.     private bool control1 = false;
    13.  
    14.     //Controls access to  the first step
    15.     private bool control2 = false;
    16.  
    17.     //Controls access to  the second step
    18.     private bool control3 = false;
    19.  
    20.  
    21.     /// <summary>
    22.     /// Font setting where the caret aligns naturally.
    23.     /// Default: 14 (Arial)
    24.     /// </summary>
    25.     public int workingFontSize = 14;
    26.  
    27.     /// <summary>
    28.     /// The ratio used to move the caret relative to the font size.
    29.     /// Default: 0.84 (Arial)
    30.     /// </summary>
    31.     public float ratioToFont = 0.84f;
    32.  
    33.     //Update Method
    34.     void Update(){
    35.  
    36.         if (control1 == false) {
    37.  
    38.             if (control2 == false) {
    39.  
    40.                 InputField temp = gameObject.GetComponent<InputField> ();
    41.  
    42.                 //Causes the "InputField" to create the "Caret Object'
    43.                 temp.text = "1";
    44.          
    45.                 temp.text = "";
    46.  
    47.                 control2 = true;
    48.  
    49.             } else if (control3 == false) {
    50.  
    51.                 ResetCaret ();
    52.          
    53.                 control3 = true;
    54.                 control1 = true;
    55.  
    56.             }
    57.  
    58.         }
    59.  
    60.     }
    61.  
    62.     /// <summary>
    63.     /// Resets the caret.
    64.     /// Accesses the "Local Position" of the caret Object
    65.     /// and changes the "Y Value" based on the formula in
    66.     /// the "ScaleCaret()" method
    67.     /// </summary>
    68.     public void ResetCaret(){
    69.  
    70.         string name = gameObject.name + " Input Caret";
    71.  
    72.         Transform temp1 = gameObject.transform;
    73.  
    74.         GameObject temp2 = temp1.Find (name).gameObject;
    75.  
    76.         Vector3 temp3 = temp2.transform.localPosition;
    77.  
    78.         temp3.y = temp3.y + ScaleCaret();
    79.  
    80.         temp2.transform.localPosition = temp3;
    81.  
    82.  
    83.     }
    84.  
    85.     /// <summary>
    86.     /// Uses the "workingFontSize" and "ratioToFont to
    87.     /// generate a float value to correct the loaction
    88.     /// of the "Input Field" caret.
    89.     /// </summary>
    90.     /// <returns>New float to add to the Y pos of the caret.</returns>
    91.     private float ScaleCaret(){
    92.  
    93.         InputField temp1 = gameObject.GetComponent<InputField> ();
    94.  
    95.         Text temp2 = temp1.textComponent;
    96.  
    97.         int temp3 = temp2.fontSize;
    98.  
    99.         float temp4 = (float)(temp3 - workingFontSize) * (ratioToFont);
    100.  
    101.         return temp4;
    102.  
    103.     }
    104.  
    105. }
    106.  
     
    Southgarden116 and Xarbrough like this.