Get lines of a UI text in Unity3D - unity3d

I use the following code to get everyline of a text that I have in Text. but the code does not return the lines that are showing in the game, it returns what I have entered in inspector.
myText = GetComponent<Text>();
string[] lines = myText.text.Split('\n');
try{print(lines[0]);}catch{}
try{print(lines[1]);}catch{}
//real output :
//New Text
//What I expect:
//New
//Text
There is a picture :
Screen Shot
How can I get lines that are in the game window?
This is my first post, so excuse any mistakes

The text is displayed as two lines because of the the Text.horizontalOverflow property of the Text component is set to Wrap, but that not means it has been changed to two lines like "New \nText". It is still "New Text".
If you set horizontalOverflow to Wrap, Text will word-wrap when reaching the horizontal boundary. So the output becomes two lines in your case.
You need to set it to Overflow so the text can exceed the horizontal boundary. Then the text will not be influenced by the border of the text GameObject.

Is the same text, the problem is that the width of the RectTransform of the Text Object is too small, increase the width or decrease the text size.

Related

Unity GUI Label text justification issue

I'm running into an issue with GUI.Label in Unity3D. If I display some text on the screen and display that EXACT same string 1 pixel down and 1 to the right, it works correctly 99.9% of the time:
var old=GUI.color;
var shadow=rec;
shadow.x++; shadow.y++;
GUI.color=Color.black;
GUI.Label(shadow, txt, style);
GUI.color=old;
GUI.Label(rec, txt, style);
I've had this same code for years but I just recently discovered that a particular string will wrap text in the wrong place on the shadow text, even though they both have the same width in their rect's and same style. What's even more strange is it won't do this every frame. The text appears when I hover over something with my mouse and moving the mouse slightly will cause the shadow string to go back & forth, wrapping two different ways causing the shadow text to not coincide with the normal colored text.
Not an edge-of-screen issue either. I'm using 2020.2.1f1.
Thanks in advance!

How to block align text in SwiftUI Text View?

There is left, center and middle aligned text.
Yet no block alignment like in other Word Processors.
How can one align text in a view using block format?
For example, if you put text in a label, you can justify the text contained in this way:
label.textAlignment = NSTextAlignment.Justified

VBA Word o set textbox text/paragraph vertical alignment to "center"

I have created a textbox in Word and can set various properties for it, such as position, text, text style, text horizontal alignment (left/right/center), color, and so on.
But I cannot find any settings to set the vertical alignment of text in the box to Top/Middle/Bottom. There's a button for vertical alignment on the ribbon to set that property, but I can't find it through the object inspector, through Intellisense, or through searching the net. I tried recording a macro, but the only line that showed up in the macro was the VBA line to select the textbox. Nothing else. :-(
The ribbon button is beside the "set text direction" option, but I couldn't find VBA for that setting either. I also tried the Textframe2 property, but saw nothing in there for vertical alignment.
Here's the code that sets the horizontal alignment of text. Also, I included the enum that I think I need to use. But I can't find the property to accept a value from the enum.
tbox.TextFrame.TextRange.ParagraphFormat.Alignment = wdAlignParagraphRight
WdVerticalAlignment enumeration (Word):
Name Value Description
wdAlignVerticalBottom 3 Bottom vertical alignment.
wdAlignVerticalCenter 1 Center vertical alignment.
wdAlignVerticalJustify 2 Justified vertical alignment.
wdAlignVerticalTop 0 Top vertical alignment.
Does anyone know the syntax for the property that I need to set to vertically align text inside a textbox shape? Thank you
The property is TextFrame.VerticalAnchor that uses the enumeration MsoVerticalAnchor
For example:
ActiveDocument.Shapes(1).TextFrame.VerticalAnchor = msoAnchorMiddle
(The enumeration mentioned in the question is for page layout.)

Text component displaying lines in reverse order

Alright, I do not know how to fix this and only ran into this problem after trying to put in some longer text on a UI Text component. I have tried both pasting a value into its Text attribute through the Unity editor, and setting its value programmatically like this:
t.GetComponent<Text> ().text = "This is where meat, fish, and [...] (long text)"
Because Horizontal wrap is on, the text wraps when it reaches the edge of the available space.
However, the text displays backwards. Meaning, the start of the paragraph is at the bottom, and bottom at the top. Taking off wrap fixes this, but then the font size has to be really small (or it won't all be visible), and it can't form a normal paragraph because it has to... you know... wrap.
Is this a bug in Unity?
This is what happens - as you can see, it is displayed backwards:
The negative Line Spacing value is what is causing the issue here:
When the value of this field is less than 0, the lines will actually be arranged backwards, leading to the behaviour you're currently encountering. Just change the value to a positive number to have the text display correctly.

Why does the Label text not always start from the same point?

I have a UILabel which has 4 lines. Its font size is 15 & autoshrink is enabled. It's minimum font size is 10.
When my text is too long then it automatically starts from first line. But when my text is too small then it starts from middle line.
How can I make sure the text always starts from first line, whether it's too long or too short?
I think what you're looking for is more a UITextView than a UILabel.
From Apple's doc :
UITextView displays a region that can contain multiple lines of editable text.
The editable part is optional. You can disable it, along with scrolling if you want to be close to a UILabel.