Version: 2017.3 (switch to 2017.4)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Text.alignment

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public var alignment: TextAnchor;
public TextAnchor alignment;

Description

The positioning of the text reliative to its RectTransform.

This is the positioning of the Text relative to its RectTransform. You can alter this via script or in the Inspector of a Text component using the buttons in the Alignment section.

no example available in JavaScript
//Create a Text GameObject by going to Create>UI>Text. Attach this script to the GameObject to see it working.

using UnityEngine; using UnityEngine.UI;

public class UITextAlignment : MonoBehaviour { Text m_Text;

void Start() { //Fetch the Text Component m_Text = GetComponent<Text>(); //Switch the Text alignment to the middle m_Text.alignment = TextAnchor.MiddleCenter; }

//This is a legacy function used for an instant demonstration. See the UI Tutorials pages and UI Section of the manual for more information on creating your own buttons etc. void OnGUI() { //Press this Button to change the Text alignment to the lower right if (GUI.Button(new Rect(0, 0, 100, 40), "Lower Right")) { m_Text.alignment = TextAnchor.LowerRight; }

//Press this Button to change the Text alignment to the upper left if (GUI.Button(new Rect(150, 0, 100, 40), "Upper Left")) { m_Text.alignment = TextAnchor.UpperLeft; } } }

Did you find this page useful? Please give it a rating: