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

How do you change the text alignment vertically and horizontally?

Discussion in 'UGUI & TextMesh Pro' started by rapidrunner, Mar 21, 2016.

  1. rapidrunner

    rapidrunner

    Joined:
    Jun 11, 2008
    Posts:
    944
    I can't find references to how to change via code, the alignment of text in a text field.

    In the editor you simply click buttons, but what if you want to change it via code?
     
  2. rapidrunner

    rapidrunner

    Joined:
    Jun 11, 2008
    Posts:
    944
    Solved the problem. Quite obscure; not sure why.

    So, a text component expose most of its parameters, like font, font size, horizontal and vertical overflow and so on, but for the alignment it works differently.

    To do so; you need to use the alignment property of the text component, which accept Enum of type "TextAnchor".

    So, to assign a different value, the correct way is

    Code (CSharp):
    1. textfield.alignment = TextAnchor.UpperLeft;
    The manual once again was not really helpful; since the values returned from a search, are related to the legacy GUI

    Hopefully this will help others that are confused as me
     
  3. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    It's not so obscure. You'll get far by learning the way Visual Studio gives hints. Coming across to enum's like this all the time:

    type.png
     
  4. rapidrunner

    rapidrunner

    Joined:
    Jun 11, 2008
    Posts:
    944

    I've been working for quite a long time on VS, and as you pointed out in the picture; you can see that the enum is not mentioned anywhere. Either you have magic to know what that function accept as parameter; or you call the get and save it in a variable, to figure out what that variable is (which is what I did).

    All that you see is the function signature and a brief description; VS does not do magic for you :)

    Unity docs are lacking in so many aspects, but I understand that they do not have time to explain everything, because that's what forums are about. Which is fair for me.
     
  5. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    It does say alignment is defined with type TextAnchor. By typing "alignment." and "TextAnchor." you'll get another list of possibilities as in screenshot. I'd start deducing with the "alignment." to see if it has a setter function, but in this case it might show nothing, so it would mean it expects a direct assignment from the class. Sometimes it means a "new" constructor, but class consts and enums are also there.

    Just mentioning, lots of information without needing to find documentation.
     
  6. rapidrunner

    rapidrunner

    Joined:
    Jun 11, 2008
    Posts:
    944
    I did get your point, but you call that "straightforward"?

    The manual:

    The property is called "alignment", the UI in the editor place it under "Paragraph"; but if you think about anchor, you envision the positioning anchors, used when you stretch the control.

    If you have a mentality that follow the association of anchor with alignment, then the passge is pretty quick; I guess I am more inclined to see a 1:1 relation between what the editor show, and what I would expect in the API; which is why I have hard time sometimes, to find how to use a specific control.

    The manual and docs are pretty good (compared to MS horrible site, it is heaven); but in many cases, checking a 2 line examples on Unity answers is faster and clearer.

    On top of that; my main objective is not to learn sonething by exploring; but to get stuff done; which is why I rely heavily on a manual and practical examples. As much as it entertain me to play detective and figure things out; time is money; so I can't afford to have someone to pay for time spent "figuring out" things.

    This is my approach of course; if I was younger and with more free time, I would gladly take the challenge and have fun figuring stuff out.
     
    grossimatte likes this.