Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: vei on December 28, 2012, 02:39:25 AM

Title: Justify Alignment for Text
Post by: vei on December 28, 2012, 02:39:25 AM
Hi, sorry if this question has been asked before.

How can I make justify alignment in UILabel? Can I put it as future feature request?
Thanks a lot!
Title: Re: Justify Alignment for Text
Post by: ArenMook on December 28, 2012, 11:59:57 AM
There is no justify alignment with UILabel, and unfortunately it's not currently on the roadmap.
Title: Re: Justify Alignment for Text
Post by: ManyWorlds on February 19, 2013, 11:26:38 AM
Hi!

I´m currently in the need of having this feature too, I don´t mind implementing it myself, but I will need some advice on how to proceed and what are the suggest steps.

Thanks!
Title: Re: Justify Alignment for Text
Post by: sandolkakos on February 19, 2013, 04:06:11 PM
Maybe using different UILabels with different Pivots.
Or also using various UIAnchors, one for each Widget Group.

In my images bellow i used one UIAnchor for each Widget Group.
My technique was used to Sprites, but works pefectly to UILabels too.
Title: Re: Justify Alignment for Text
Post by: vei on February 28, 2013, 05:42:00 AM
I did it before by modifying Print function from UIFont.cs.

Basically it's similar with Align function that's applied when the alignment is not Alignment.Left.
Divide the "extra spacing" left in that particular line by the number of space character.
Then just adjust the x-position of each character's buffer "verts" accordingly with that extra spacing.
Title: Re: Justify Alignment for Text
Post by: basil_11 on March 14, 2013, 10:47:41 PM
how can we find the extra line spacing?

It would be a big help for us to know how you did the justification of the Text :)

thank you sir
Title: Re: Justify Alignment for Text
Post by: DCassy on July 16, 2013, 07:03:25 AM
Hello there,

Thanks for clues, but creating text justification in UIFont seems really complicate for a non coder like me... I am so annoyed this function is not integrated in NGUI. Why isn't on the road map ? Any help would be highly useful here.
Title: Re: Justify Alignment for Text
Post by: ArenMook on July 16, 2013, 10:47:01 AM
Because NGUI is not a word processor. It's a game UI system.
Title: Re: Justify Alignment for Text
Post by: DCassy on July 16, 2013, 11:26:02 AM
 Well, I could cite several game that use huge amount of justified text... I could understand if there is a technical difficulty on doing this, but I am a bit surprised by this rude answer. I spent 90 € on your asset, to hear that I am using Unity as a word processor... Bad publicity.
Title: Re: Justify Alignment for Text
Post by: ArenMook on July 17, 2013, 03:58:47 AM
But that's the thing though, if it was a frequently requested feature, I would see no reason not to add it. I've only had this question asked maybe once every 6 months, which tells me that few games need this feature. I'm a solo developer with very limited amount of time and resources, so I have to focus on higher priority tasks -- such as redoing the font system within Unity itself in order to make it work with dynamic fonts correctly. I don't intend to be rude -- merely realistic.
Title: Re: Justify Alignment for Text
Post by: DCassy on July 18, 2013, 04:47:10 AM
I understand better now. Thanks for your answer.
Title: Re: Justify Alignment for Text
Post by: VapapeurTeam on October 07, 2013, 10:10:43 AM
Hello,

Sorry for necro'ing an old thread, but I figured it'd be better than creating a new one.

I would also like to have text justification for some UILabels in my game. Texts can look pretty awful if they're a bit lengthy (like more than 2 lines) and not justified properly.

Is it still not planned to add this feature into NGUI ? If not, I completely understand, although if you have suggestions as how you think it'd be done best in your code, I'd be happy to hear them.

Thanks in advance
Title: Re: Justify Alignment for Text
Post by: ArenMook on October 08, 2013, 02:06:33 AM
Still no plans at this time.
Title: Re: Justify Alignment for Text
Post by: mremus on November 11, 2013, 07:21:18 AM
I will add my vote for needing text justification/alignment in NGUI.  Working on a sim for the US Navy and as you would guess they like things to line up nicely in the text portion of the sim instructions.

Thanks!
Title: Re: Justify Alignment for Text
Post by: renanse on November 12, 2013, 04:08:40 PM
Justify would be very nice.  I've had to mix in another library (TextBox) just so I can do a proper opening crawl (http://en.wikipedia.org/wiki/Star_Wars_opening_crawl) in our game.  I would *really* love to replace that with NGUI text instead.
Title: Re: Justify Alignment for Text
Post by: g2mediagroup on November 12, 2013, 08:30:32 PM
I will add to the vote for text justification tools! That has been assumed on almost ALL text related programs for many years...just say'n...not trying to complain, but...guess I just did! ;)
Title: Re: Justify Alignment for Text
Post by: ArenMook on November 13, 2013, 08:05:50 PM
Planned. Soon. :)
Title: Re: Justify Alignment for Text
Post by: renanse on November 15, 2013, 04:57:55 PM
Yay!! :)
Title: Re: Justify Alignment for Text
Post by: renanse on November 22, 2013, 11:49:52 AM
Any chance the character spacing added recently is related to justify?   ;D
Title: Re: Justify Alignment for Text
Post by: ArenMook on November 22, 2013, 03:58:55 PM
Sorry no... I haven't tackled this yet. Character spacing is just that -- spacing on every single character. Justification involves adding spaces only between certain characters, so it's basically a "post-process" on a line of text -- so it's a different task.
Title: Re: Justify Alignment for Text
Post by: sunspider on December 17, 2013, 05:17:16 PM
Wow. NGUI seems so feature packed... it's sort of amazing that I can't justify my text   :-\
Title: Re: Justify Alignment for Text
Post by: pretender on December 17, 2013, 06:07:08 PM
This would be great!
Title: Re: Justify Alignment for Text
Post by: renanse on January 04, 2014, 11:55:08 AM
Bump.  Maybe you could point me to the best place to implement this and I could take a crack at it?
Title: Re: Justify Alignment for Text
Post by: ArenMook on January 04, 2014, 12:24:54 PM
Horizontal text alignment is done in a single function: NGUIText.Align. The tricky thing about justified alignment is that you'd want to modify vertices 4 at a time rather than individually, and to position them in a manner that spaces them out. The reason I haven't done this already is because Unity's TextAlignment enum doesn't have "justified" in it. Just left, center and right. My own fault for using it in the first place I suppose.
Title: Re: Justify Alignment for Text
Post by: renanse on January 04, 2014, 12:42:47 PM
It looks as though you aren't using that enum in a way where it would be relatively painless to switch it out for a custom enum.  It's not a field on any of the UI widgets, etc. at least, so no prefab issues with switching types. 
Title: Re: Justify Alignment for Text
Post by: Unisip on January 15, 2014, 05:12:15 AM
+1 for text justification
I read in the thread that it is planned, and I'm eager to have it integrated, as I'll need it very soon :)
Title: Re: Justify Alignment for Text
Post by: c4digit on January 23, 2014, 12:27:44 AM
I created a quick hack for it. It works but haven't tested for speed with large text. It also overwrites the Left enum, but could easily add a Justify enum
UIFont.cs
public void Print
add the following
         int textLength = text.Length;

         // lets figure out justification here should probably do it earlier if this is too slow
         int maxlineWidth = 0;
         int currentLineWidth = 0;
         int numberSpaces = 0;
         List<int> lineWidths = new List<int>();
         List<int> lineSpaces = new List<int>();
         for (int i = 0; i < textLength; ++i) {
            char c = text;
            if (c == '\n') {
               lineWidths.Add(currentLineWidth);
               lineSpaces.Add(numberSpaces);
               if (currentLineWidth > maxlineWidth)
                  maxlineWidth = currentLineWidth;
               currentLineWidth = 0; numberSpaces = 0;
            }
            else {
               if (c == ' ')
                  numberSpaces++;
               BMGlyph glyph = mFont.GetGlyph(c);
               if (glyph != null)
                  currentLineWidth += glyph.advance;
            }
         }
         int lineArrayPos = 0;
         int spaceExtra = 0;
         if (lineWidths.Count > 0)
            spaceExtra = (maxlineWidth - lineWidths[lineArrayPos]) / lineSpaces[lineArrayPos];

then inside if (c == '\n')
               lineArrayPos++;
               if (lineArrayPos < lineWidths.Count && lineSpaces[lineArrayPos] != 0)
                  spaceExtra = (maxlineWidth - lineWidths[lineArrayPos]) / lineSpaces[lineArrayPos];
               else
                  spaceExtra = 0;

then find later down
               if (c == ' ')
               {
                  x += mSpacingX + glyph.advance + spaceExtra; // added spaceExtra at end
                  prev = c;
                  continue;
               }
Title: Re: Justify Alignment for Text
Post by: renanse on January 31, 2014, 04:48:48 PM
Neat! :)
Title: Re: Justify Alignment for Text
Post by: ArenMook on February 01, 2014, 12:34:04 PM
The trick is also modifying PrintCaretAndSelection and PrintCharacterPositions, and making sure it all works with spacing, font scale adjustments, and UIRoot scale adjustments. But still, nice, thanks.
Title: Re: Justify Alignment for Text
Post by: leothenorseman on February 19, 2014, 05:19:41 PM
Hi, I became a member just so I could request justified text. I am UI artist for a game company, and my colleagues and I often wish we could justify text. It's a part of graphic design, so it is often required for layouts. Thanks!
Title: Re: Justify Alignment for Text
Post by: ArenMook on February 20, 2014, 09:03:14 AM
Already added in 3.5.0.
Title: Re: Justify Alignment for Text
Post by: renanse on March 07, 2014, 12:46:06 PM
And I forgot to say thank you.  So, "Thank you, Aren." :)
Title: Re: Justify Alignment for Text
Post by: tistik on July 13, 2016, 03:44:31 AM
Hi there,

I was wondering if some options could be added to the justification text feature. Right now the spacing and the justify option seem to be in some sort of competition, which in some cases gives correctly justified text and in some other cases awfully justified text, where the space between characters varies widely from a word to another. I suppose this variation is due to the justify option that stretches the words, sometimes crossing the specified spacing.

Maybe some advice to use the justify option at best ?
Title: Re: Justify Alignment for Text
Post by: ArenMook on July 13, 2016, 10:47:58 AM
Justification will always be uneven by the nature of it, as the characters need to be aligned to pixels in order to appear crisp. For example a line of 10 characters with a label width of 50, but characters only take a total width of 44 means 6 pixels need to be added in between of characters. You can't just separate each of the 10 characters by 0.6 as that will make them blurry. Instead, only 6 of the 10 characters will gain spacing between them.