Author Topic: Justify Alignment for Text  (Read 37300 times)

g2mediagroup

  • Guest
Re: Justify Alignment for Text
« Reply #15 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! ;)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Justify Alignment for Text
« Reply #16 on: November 13, 2013, 08:05:50 PM »
Planned. Soon. :)

renanse

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 1
  • Posts: 14
    • View Profile
Re: Justify Alignment for Text
« Reply #17 on: November 15, 2013, 04:57:55 PM »
Yay!! :)

renanse

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 1
  • Posts: 14
    • View Profile
Re: Justify Alignment for Text
« Reply #18 on: November 22, 2013, 11:49:52 AM »
Any chance the character spacing added recently is related to justify?   ;D

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Justify Alignment for Text
« Reply #19 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.

sunspider

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 16
    • View Profile
Re: Justify Alignment for Text
« Reply #20 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   :-\

pretender

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 155
    • View Profile
Re: Justify Alignment for Text
« Reply #21 on: December 17, 2013, 06:07:08 PM »
This would be great!

renanse

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 1
  • Posts: 14
    • View Profile
Re: Justify Alignment for Text
« Reply #22 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?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Justify Alignment for Text
« Reply #23 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.

renanse

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 1
  • Posts: 14
    • View Profile
Re: Justify Alignment for Text
« Reply #24 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. 

Unisip

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 26
    • View Profile
Re: Justify Alignment for Text
« Reply #25 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 :)

c4digit

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
Re: Justify Alignment for Text
« Reply #26 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;
               }

renanse

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 1
  • Posts: 14
    • View Profile
Re: Justify Alignment for Text
« Reply #27 on: January 31, 2014, 04:48:48 PM »
Neat! :)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Justify Alignment for Text
« Reply #28 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.

leothenorseman

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
Re: Justify Alignment for Text
« Reply #29 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!