Tasharen Entertainment Forum

Support => NGUI 3 Documentation => Topic started by: ArenMook on November 20, 2013, 09:46:54 PM

Title: UILabel
Post by: ArenMook on November 20, 2013, 09:46:54 PM
Overview

UILabel is a Widget (http://www.tasharen.com/forum/index.php?topic=6702) that can be used to display text.

(http://www.tasharen.com/ngui/uilabel.jpg)

All labels require a Font to work with. This font can be Dynamic (directly referencing a Unity Font), or it can be a Bitmap font -- a font embedded within an Atlas. Dynamic fonts are more robust as they don't require you to pre-generate your glyphs before hand, but Bitmap fonts can be drawn within the same draw call as the rest of your atlas, and can be decorated nicely in an image editing tool such as Photoshop.

You can change the alignment of the labels simply by switching the Pivot point. Top-left, Left and Bottom-left pivot point will result in a left-aligned text. Top, Center, or Bottom alignment will cause the text to be centered, and Top-right, Right or Bottom-right pivot will make your text right-aligned.

With the Dynamic font chosen you can set the Font Size as well as style directly on your label. You can also set the material that will be used to draw it, if you wish.

The big box is -- as you probably guessed -- where you enter text. It's a multi-line text box by default, unless limited by the Max Lines property below it.

Overflow handling lets you determine what happens when the label's text exceeds the allowed space.

(http://www.tasharen.com/ngui/overflow.jpg)
The Spacing field lets you adjust the distance between characters. Both positive and negative values are allowed. This value is in pixels.

Max Lines, as mentioned earlier, lets you control how many lines you want there to be at maximum. You can leave it at zero if you want it to be unlimited.

You can turn off Encoding if you don't want color tags and emoticons to be processed. Input fields do this by default.

If you want, you can give your labels a Gradient by specifying the bottom and top colors.

You can give your text a shadow or an outline Effect, but note that doing so will double the geometry in case of shadow, and multiply it by a factor of 5 in case of outline -- so be mindful of this feature. The Distance parameter controls how far the shadow or outline is from the base text, in pixels.

To change the label's text at run-time, you can do the following:
  1. UILabel lbl = GetComponent<UILabel>();
  2. lbl.text = "Hello world!";

Pro-Tip #1

You can add bold, italic, underline, and other effects to your label by using bbcode syntax like so:

[b]bold[/b]
[i]italic[/i]
[u]underline[/u]
[s]strikethrough[/s]

You can also embed clickable links in your labels like so:

[url=Some Message or Link]Click Me[/url]

To retrieve what you clicked on, attach a box collider to your label (ALT+SHIFT+C) and a script that has a function like this in it:
  1. void OnClick ()
  2. {
  3.     UILabel lbl = GetComponent<UILabel>();
  4.     string url = lbl.GetUrlAtPosition(UICamera.lastWorldPosition);
  5.     Debug.Log("Clicked on: " + url);
  6. }

Pro-Tip #2

You can give your labels a beveled look by specifying a dark foreground color and a bright Shadow effect.

(http://www.tasharen.com/ngui/bevel.jpg)

Pro-Tip #3

You can make the text ignore the label's color tint by using the [c]text here[/c] tag, and change the alpha by using [Aa] syntax, like "[7f]".

Class Documentation

http://tasharen.com/ngui/docs/class_u_i_label.html

If you have a question regarding this component or would like me to clarify something, just post a reply here.
Title: Re: UILabel
Post by: dkozlovtsev on November 28, 2013, 08:59:41 AM
Emoticons for UILabels with dynamic fonts, is it possible?
Title: Re: UILabel
Post by: ArenMook on November 28, 2013, 03:52:45 PM
Not in 3.0.6. Likely 3.0.8.
Title: Re: UILabel
Post by: Messu on December 04, 2013, 05:02:18 AM
I would like to know if Dynamic Fonts increase dramatically the number of draw calls or if many labels using the same Dynamic Font will be drawn on the same draw call.
Title: Re: UILabel
Post by: ArenMook on December 04, 2013, 05:31:52 PM
If you make dynamic font using labels follow each other in their depth order, they will all be packed into one draw call.
Title: Re: UILabel
Post by: memorygarden on December 07, 2013, 02:25:07 AM
HI , label.printedSize is not correct? i think the label.printedSize is not the size on sense?

i have some labels in a list , and a background, how could i know the background's localSize?

thank you!
Title: Re: UILabel
Post by: ArenMook on December 07, 2013, 03:14:12 AM
UILabel.printedSize returns the size of the label in pixels. This value is not the final size on the screen, but is rather in NGUI's virtual pixels. If the UIRoot is set to PixelPerfect, then this value will match the size in screen pixels. If the UIRoot is FixedSize, then you need to scale the printedSize by UIRoot.pixelSizeAdjustment.
Title: Re: UILabel
Post by: memorygarden on December 07, 2013, 04:38:05 AM
first,thank you for your reply.

but now the result is also looks like incorrect;

i dit as follows:


UILabel ul = goo.GetComponent<UILabel>();
         
Debug.Log(UIRoot.GetPixelSizeAdjustment(goo));
Debug.Log (ul.printedSize);
Debug.Log(new Vector2(ul.printedSize.x/UIRoot.GetPixelSizeAdjustment(goo),
            ul.printedSize.y/UIRoot.GetPixelSizeAdjustment(goo)));


Console out:

2.416107
(490.0, 20.0)
(202.8, 8.3)



i think the label's printSize in sense is bigger than (202.8, 8.3)

did i made some mistakes?


thank you
Title: Re: UILabel
Post by: ArenMook on December 07, 2013, 04:09:52 PM
You need to multiply, not divide.
Title: Re: UILabel
Post by: memorygarden on December 08, 2013, 02:31:45 AM
Multiply? The localSize I saw in the sense was about 240. after multiply the value will be 400+. It's not correct . oh my god . Please help me . Thank you .
Title: Re: UILabel
Post by: kitgui on December 16, 2013, 05:25:17 PM
Is there a current guide on how to do emoticons?

EDIT : Nevermind, I found the Chat example and saw how you did it :)
Title: Re: UILabel
Post by: zipper on December 18, 2013, 12:53:20 AM
It would be wonderful to distribute UILabel text along a 2d spline.

Maybe have a 2d spline component and attach the text to it, or the other way around.
Title: Re: UILabel
Post by: kLy on December 19, 2013, 04:28:36 PM
Is there a way to set the font size independently of font resolution?

Right now I'm using a 3D GUI and often moving text elements right up to the camera. In such a case the fonts look horribly pixelated if the font size is low. The only way to fix this is to set the font size up, but setting the font size up also increases the actual size of the text.

The only workaround I've found now is to crank up the font size, then reduce the dimensions while using "ShinkContent", but this is an ugly hack and results in unpredictable text sizes in multiline labels.
Title: Re: UILabel
Post by: dkozlovtsev on December 20, 2013, 04:03:14 AM
@kLy

Last time i checked it was still possible to use scale to conrol widget size it really helps with Dynamic Font labels, but certainly it is not the prittiest of solutions
Title: Re: UILabel
Post by: ArenMook on December 20, 2013, 12:35:51 PM
Nothing wrong with scaling it by adjusting the transform.
Title: Re: UILabel
Post by: moursbourn on December 30, 2013, 10:18:31 PM
color via the [color] [-] flag is being multiplied with the font color.   I seem to remember this used to replace the color. Is this a bug or intended?  It seems much more useful as a replacement, even if it does replace the gradient.
Title: Re: UILabel
Post by: ArenMook on December 31, 2013, 07:29:57 AM
Intentional. Widget's color tint affects everything. Do something like this instead now:
  1. label.text = "[000000]Black [ff0000]Red";
Title: Re: UILabel
Post by: jinsediaoying on January 10, 2014, 02:46:40 PM
do this support custom font that create via Assets->create-> custom font?
UILabel can recognize it as a dynamic font, but throws a tons of errors
Title: Re: UILabel
Post by: ArenMook on January 10, 2014, 06:20:51 PM
No, custom font feature is something new in Unity and I have not yet looked at it.
Title: Re: UILabel
Post by: AGB on January 21, 2014, 02:16:48 AM
What are the tags for new font modify functions? maybe there are more?
Title: Re: UILabel
Post by: ArenMook on January 21, 2014, 04:17:04 AM
BBcode style:

[b]bold[/b]
[i]italic[/i]
[u]underline[/u]
[s]strikethrough[/s]

...etc. Look inside NGUIText.ParseSymbol.
Title: Re: UILabel
Post by: dannygame2012 on January 22, 2014, 09:28:45 PM
I want to add an emoticon to my dynamic font ? can I?
Title: Re: UILabel
Post by: ArenMook on January 23, 2014, 07:11:06 AM
Not yet.
Title: Re: UILabel
Post by: dannygame2012 on January 23, 2014, 11:32:18 PM
next version can support this feature?
Title: Re: UILabel
Post by: ArenMook on January 24, 2014, 10:44:43 AM
A day later? Still not yet.
Title: Re: UILabel
Post by: dbuck on January 27, 2014, 10:54:47 AM
Hey Aren, Is there anything set up to do Bulleted text?

Thinking I'll parse the lines for the beginning bullet char: '\u2022' and split those lines into a separate indented label, and place the whole listing of the text copy within a table. Is there currently an easier way to get that effect?

Quote
Titled
  • Yea.
  • These are fun
And copy
Title: Re: UILabel
Post by: ArenMook on January 27, 2014, 07:05:58 PM
No, there is nothing in NGUI that would parse bullet text / paragraphs.
Title: Re: UILabel
Post by: ury2ok2000 on January 30, 2014, 12:49:25 PM
I hope this is the right place to ask this. I am trying to get a label to appear over a game object. However when I try to set the position it always appears at 0,0,0 (center of the screen). The only exception is if I change the label position in the script's update.

I am instantiating a label prefab with the text script attached.
Title: Re: UILabel
Post by: ArenMook on January 31, 2014, 12:40:44 PM
Label should remain in your UI hierarchy. You can anchor widgets to 3D objects quite easily. Consult the UIRect documentation page (http://www.tasharen.com/forum/index.php?topic=7013.0) for details.
Title: Re: UILabel
Post by: Denis on February 13, 2014, 02:25:28 AM
From glancing at documentation and very briefly skimming the code, it seems there are no formatting controls that would allow me to do something like a table. E.g.

  Column1   Column2  Column3
  Value1     Value2    Value3

What do you recommend for me to be able to achieve this?  Offhand, some options I can think of:

A) use 3 overlapping UILabels, clipping to mutually exclusive spaces.
B) use a monospaced font and pad with spaces. Actually, will this work? I did a quick test and it seems that padding with spaces doesn't work.
C) modify code and add new formatting controls. E.g. [150] means jump to 150 pixel offset.  This is the solution I've implemented in a couple of other engines. This would allow me to do a table with one UILabel. Not sure how complex this addition is, though. Seems like it should be straightforward if I know the places to hook into that do positioning.  What are those hooks?

thanks








Title: Re: UILabel
Post by: ArenMook on February 13, 2014, 03:21:49 AM
D) http://www.tasharen.com/forum/index.php?topic=6758.0

Although generally I'd suggest using a prefab for what a single row would look like, and instantiating it multiple times under a UIGrid (and changing the content on that row as necessary)
Title: Re: UILabel
Post by: Denis on February 13, 2014, 04:20:30 PM
I tried using a Row Prefab with a UIGrid, and it certainly works but I am concerned about performance and memory. That's a lot more GameObjects and Components.  It would make sense if I was doing a special screen, but I am trying to use this for debug text that I don't want to impact the framerate.  It would be more efficient (and faster to implement) if I could use a single UILabel component.

I suspect option 'C' may be not too bad, since just need to hook into where it computes glyph offsets.

Title: Re: UILabel
Post by: ArenMook on February 14, 2014, 06:11:00 AM
Having a few extra game objects won't give you any noticeable performance changes. Once you hit like 10,000+ game objects... then start worrying. Also if you're truly worried about minimizing everything, you can just have a paged approach where you load only a few rows at a time. I did that in Starlink -- I only load rows that are close to the player's score.
Title: Re: UILabel
Post by: Denis on February 14, 2014, 12:04:27 PM
True, but am thinking more implementation time or hassle. If you're just trying to implement something quickly, such as on-screen debug text, it's a lot faster to do it with one UILabel. The following would be similar to how tabs (\t) work where [+100] jumps to 100 pixels from left of element.
1) add UILabel object
2) add code that outputs something like "value[+100]value[+200]value\nvalue[+100]value[+200]value"

Compare to with a UIGrid and row Prefab.
1) add a Prefab GameObject
2) add one UILabel per column, with appropriate dimensions
3) add UIGrid object
4) add code that looks like:
* AddChild(prefab)
* UILabel[] columns = GetChildren()
* columns[0].text = value; columns[1].text = value; columns[2].text = value;

Once the prefab is setup, I'll grant it's pretty easy to use and work with.  But if you want something quick & dirty, the "tabs" approach is better.
Title: Re: UILabel
Post by: sathyaraj on February 24, 2014, 04:01:32 AM
Is it possible to change font size inside UILabel. Something like this instead of using two separate labels. "Different font size" If you see text "font", its small in size compared to others.
Title: Re: UILabel
Post by: ArenMook on February 24, 2014, 05:07:30 PM
You can use the [sub] tag to shrink the text, but you can't reference different fonts mid-sentence.
Title: Re: UILabel
Post by: Tripwire on February 26, 2014, 05:38:47 AM
Hi ArenMook,

In the older versions of NGUI it was possible to call:
UILabel.password = true

To set the UILabel to show a password field instead of just plain text. Is this still possible in the latest version of NGUI? I can't seem to find how.
Title: Re: UILabel
Post by: ArenMook on February 26, 2014, 03:52:22 PM
Yes, you set it on the UIInput instead.
Title: Re: UILabel
Post by: Tripwire on February 27, 2014, 04:21:22 AM
Yes, you set it on the UIInput instead.

Thx for your reply.

I don't need an input field there, I have a ATM machine (sort of) and the numbers are added through buttons not with an input text field. I've attached the InputField script to my label and set it to password. But when i'm adding numbers by pressing on the buttons they aren't **** but just 1234.
Title: Re: UILabel
Post by: ArenMook on February 27, 2014, 06:49:12 PM
If you are setting your label's text directly, then just replace what you're setting with stars.
Title: Re: UILabel
Post by: angrypenguin on March 05, 2014, 11:58:48 PM
Overflow handling lets you determine what happens when the label's text exceeds the allowed space.
Under what conditions? I've got a bunch of widgets here which seem to partially apply the setting when I select "Resize Height" but then snap back to ShrinkContent.

Edit: Righto, it was having the anchor set to anything other than "None". While this makes sense from a nuts and bolts perspective it's a pretty counter-intuitive workflow, and my experience with the new anchor system thus far is that this isn't the only place where it has that effect. Another example is moving a parent object who's children have anchors on them. In each case I'd expect the anchor to be updated. After all, why would I want something to resize height and expect it to honor a specified height at the same time? Or why would I want to move a parent object and not have its children come for the ride?
Title: Re: UILabel
Post by: ArenMook on March 06, 2014, 12:18:09 PM
Anchors do get updated as long as the children are anchored to the parent you're moving, not to some external source.
Title: Re: UILabel
Post by: bearworks on March 10, 2014, 07:09:22 AM
Can we give some offset to underline ,like this : [u=0.1]...[/u] ?
Title: Re: UILabel
Post by: ArenMook on March 10, 2014, 09:19:59 PM
The underline is always based where the "_" character is located.
Title: Re: UILabel
Post by: blitzer on March 10, 2014, 09:22:50 PM
I was wondering if there was any way to have "multiple" text aligments in 1 UILabel (I'm guessing no). I know that I would accomplish this by using multiple labels, I just thought it would be nice if I could code this in 1 label.

Ex.
Blah blah lots of text explaining something, here is your score:(aligned left, clamp content)
Score (aligned center)
More text about something else (aligned left, clamp content)
Title: Re: UILabel
Post by: ArenMook on March 10, 2014, 09:34:16 PM
There have been third-party addons that make this possible, but base NGUI doesn't support embedding alignment changes.
Title: Re: UILabel
Post by: blitzer on March 11, 2014, 02:05:37 AM
I see, is that type of feature something that you think could be added someday? If not, would you happen to remember any of those addons that supported it by chance?
Title: Re: UILabel
Post by: ArenMook on March 11, 2014, 07:53:19 PM
Eventually I will add native support for it. But for now -- http://www.tasharen.com/forum/index.php?topic=3695.0
Title: Re: UILabel
Post by: stevej on March 28, 2014, 06:26:20 AM
Intentional. Widget's color tint affects everything. Do something like this instead now:
  1. label.text = "[000000]Black [ff0000]Red";

Has this changed recently? I've used it quite a bit in 2.7 (etc), but can't get it to work in the latest version. The tag just gets ignored and the text remains white.
Title: Re: UILabel
Post by: rtargosz on March 29, 2014, 09:26:38 AM
Has this changed recently? I've used it quite a bit in 2.7 (etc), but can't get it to work in the latest version. The tag just gets ignored and the text remains white.

I think you need the termination sequence [-] like this:
[0000ff]Blue![-][00ff00]Green![-]
Title: Re: UILabel
Post by: ArenMook on March 30, 2014, 07:56:14 AM
This hasn't changed since early 3.0.X. Tint color is multiplied with the embedded color, but if the tint color is white like you mentioned, then tags should work as expected with or without the terminator ([-]).

Assuming the label has "BBCode" enabled.
Title: Re: UILabel
Post by: stevej on April 01, 2014, 01:16:59 AM
This hasn't changed since early 3.0.X. Tint color is multiplied with the embedded color, but if the tint color is white like you mentioned, then tags should work as expected with or without the terminator ([-]).

Assuming the label has "BBCode" enabled.

Hmm... yeah it's odd. I definitely have BBCode enabled on the label. It's part of a TextList - does that matter?

For example, I'm doing this in code:

  1. Feedback = GameObject.Find("Feedback").GetComponent<UITextList>();
  2. Feedback.Clear();
  3. Feedback.Add(string.Format("Welcome to location [77FF00]{0}[-].", Caption));
  4.  

And I see "Welcome to location Sandbox." on screen, but it's all white.

Sorry to keep referring to 2.7, but that's the version I've come from and the above type of code worked back then. Just trying to figure out what's changed so I can adapt :)
Title: Re: UILabel
Post by: ArenMook on April 01, 2014, 10:55:06 AM
Chat input script purposely strips the color tags. Line 44 of ChatInput.cs.
Title: Re: UILabel
Post by: stevej on April 01, 2014, 07:16:50 PM
Chat input script purposely strips the color tags. Line 44 of ChatInput.cs.

ChatInput.cs ?
Title: Re: UILabel
Post by: ArenMook on April 03, 2014, 09:57:05 PM
Yes, ChatInput: http://www.tasharen.com/ngui/docs/class_chat_input.html

That's what the chat window uses to submit text.
Title: Re: UILabel
Post by: stevej on April 03, 2014, 09:58:32 PM
Yes, ChatInput: http://www.tasharen.com/ngui/docs/class_chat_input.html

That's what the chat window uses to submit text.

Yeah, I tried to find that file in my Project but it doesn't exist. Is it maybe in the Example folders? I don't import those.
Title: Re: UILabel
Post by: ArenMook on April 03, 2014, 10:04:26 PM
Yep, it's a part of the Examples folder.
Title: Re: UILabel
Post by: stevej on April 06, 2014, 02:13:01 AM
Okay - just going back to a simple example. I've created a UILabel. Using a bitmap font. Color tint is the default white. BBCode is enabled.

If I set the "Text" property on it to anything which includes tags - e.g. "[444444]Test", or "[444444]Test[FFFFFF]", etc - then it stays all white - i.e. the tags are ignored. If I try something like "Test" then it works.

Just can't get color tags to work like I used to.
Title: Re: UILabel
Post by: ArenMook on April 06, 2014, 03:05:07 AM
Steps to repro?
Title: Re: UILabel
Post by: stevej on April 06, 2014, 07:33:32 AM
Steps to repro?

As I said - using a bitmap font, create a UILabel, BBCode is on by default, white tint is set by default, assign some text to the label that include a color tag and the color part gets ignored. The text is all white on the label.

If you switch off the BBCode checkbox, you can see the tag on the label - e.g. "[444444]Test" - and if you switch on the BBCode checkbox you just see the expected "Test", but all white.

Did this throughout 2.x NGUI, so definitely something different, just not sure if it's me or NGUI that's the problem.
Title: Re: UILabel
Post by: stevej on April 06, 2014, 07:34:10 AM
Note - the color tags work fine with a Dynamic font. Just not with a Bitmap font.
Title: Re: UILabel
Post by: tkshredder on April 07, 2014, 07:39:36 AM
Question on using UILabel components to mask a UI texture or sprite - is this possible? (Below is a UI mockup)

It would effectively be a "knockout" effect, where numbers / letters from a font could be used to punch through a background texture / sprite, creating a see-through effect.
(http://panyamedia.com/outhouse/ui%20ideas.jpg)
Title: Re: UILabel
Post by: ArenMook on April 08, 2014, 01:29:08 AM
@stevej: I am not seeing any issues:

1. New Scene.
2. ALT+SHIFT+L
3. Chose "Arimo20" font.
4. Entered "New [99ff00]Label" as text. "New" is white, "Label" is green.

@tkshredder: When using a dynamic font you can specify a custom material. If you specify a shader that does this, then sure.
Title: Re: UILabel
Post by: stevej on April 08, 2014, 01:33:15 AM
@stevej: I am not seeing any issues:

1. New Scene.
2. ALT+SHIFT+L
3. Chose "Arimo20" font.
4. Entered "New [99ff00]Label" as text. "New" is white, "Label" is green.

It has me pretty stumped. I'm using GlyphDesigner to create the fonts and then adding them to an atlas using TexturePacker. Is there any chance that somewhere in that pipeline, I'm ending up with an output file (PNG) that has some property NGUI doesn't like? I know - I might be reaching at this point...
Title: Re: UILabel
Post by: ArenMook on April 08, 2014, 01:38:48 AM
Check the shader used by your atlas. It needs to be Unlit/Transparent Colored.
Title: Re: UILabel
Post by: stevej on April 08, 2014, 05:55:27 AM
Check the shader used by your atlas. It needs to be Unlit/Transparent Colored.

Bingo! Was Unlit/Transparent. No idea how that happened.

Thanks so much for all the help - awesome support as usual :)

I'm officially problem free!
Title: Re: UILabel
Post by: MyBestAttempt on April 24, 2014, 05:31:32 PM
Hi,

I am having an issue with my game at the minute, I have successfully managed to get my UILabel to follow a game object however when I zoom in on the object it shrinks the text is there anyway I can stop this from happening?

Thanks.
Title: Re: UILabel
Post by: emo44 on April 25, 2014, 07:38:47 AM
Hi,
How to put a TAB with BBcodein a UILabel, i try [TAB]text[/TAB] but do nothing...
Title: Re: UILabel
Post by: ArenMook on April 25, 2014, 10:28:44 AM
@MyBestAttempt: Not sure what you mean by "shrinks the text". If you've simply made it follow the object, then the label will be exactly the same size as all you're doing is changing its position. This doesn't seem like a UILabel-related question.

@emo44: There is no such tag in NGUI. All the supported tags are mentioned in the original post.
Title: Re: UILabel
Post by: MyBestAttempt on April 25, 2014, 10:45:55 AM
Hi, I suppose "shrinks" isn't technically correct, when you create a label it will stay at the same fixed size regardless of the orthographic size of the primary game play camera, I assume this is because it is the NGUI UI camera which controls the rendering of the label, what I am asking is if there is a way to ensure the label will resize based on the main game cameras orthographic size? i.e. if a user performs a pinch and zoom action that the label will still correctly fill out the area I want. I have attached a screenshot showing the label at the same size when the orthographic size zoomed in.
Title: Re: UILabel
Post by: ArenMook on April 25, 2014, 10:47:35 AM
Sure. Make it be drawn by your game camera by making it be a part of the world, not a part of the 2D UI.

Either that or write a script that will scale it based on your zoom level.
Title: Re: UILabel
Post by: MyBestAttempt on April 25, 2014, 11:08:14 AM
How can I make it be a part of the world instead of the 2d UI?
Title: Re: UILabel
Post by: ArenMook on April 26, 2014, 02:23:58 PM
Add a game object to your world, attach UIPanel to it, and you can then add widgets to it like usual.
Title: Re: UILabel
Post by: qq33357486 on May 06, 2014, 10:13:17 PM
How to Set label font.
label.font is null ,so i can't use label.font.dynamicFont = myfont;
label.bitmapFont is null, so i can't use label.bitmapFont.dynamicFont = myfont;

how do i do to change the label font by code.
Title: Re: UILabel
Post by: ArenMook on May 06, 2014, 10:19:18 PM
UILabel.ambigiousFont = yourFont;
Title: Re: UILabel
Post by: qq33357486 on May 06, 2014, 11:06:37 PM
UILabel.ambigiousFont = yourFont;

But I want to change only one label.
some label has other font
Title: Re: UILabel
Post by: ArenMook on May 08, 2014, 03:18:20 AM
so... do just that? I don't understand the issue. Here's the full code, in case it's not clear:
  1. GetComponent<UILabel>().ambigiousFont = yourFont;
Title: Re: UILabel
Post by: Kamo on May 15, 2014, 09:14:33 AM
I have a suggestion value for overflow:
ClampContent with the "..." replacing the last characters at the end of the visible text
Title: Re: UILabel
Post by: yuewah on June 03, 2014, 09:27:14 AM
I have ui label of emotion icon tag [xp] that work on 3.5.x, but it doesn't work for 3.6.2.
Title: Re: UILabel
Post by: ArenMook on June 04, 2014, 02:04:33 AM
Nothing has changed in regards to emoticons in the past few months that I can think of. The emoticons that are a part of the Arimo20 font that comes with NGUI still work, for example. How can I reproduce the issue on my end?
Title: Re: UILabel
Post by: Nick_Field on June 06, 2014, 05:01:15 AM
Emoticons for UILabels with dynamic fonts, I know that isn't possible now (Emoticons == static font icons or user sprites). But Do you plan implement this feature in updates in near future?
Title: Re: UILabel
Post by: ArenMook on June 06, 2014, 10:03:06 PM
Not until Unity fixes issues with dynamic fonts.
Title: Re: UILabel
Post by: skullthug on June 12, 2014, 02:28:47 PM
Unfortunately converting my font to non-dynamic is out the question in this project. But is there a clever (or ugly) way to work around this?

Basically I just want to replace the [X] in:
[X] OPEN
with a button graphic.

The string changes so unfortunately [X] is never in the same position. Is there a way to brute force this by detecting [X]'s position and moving a UISprite to cover it or something?
Title: Re: UILabel
Post by: ArenMook on June 13, 2014, 06:16:48 AM
Not easily, no.
Title: Re: UILabel
Post by: skullthug on June 14, 2014, 12:06:51 PM
I don't expect it to be easy  ;D
Title: Re: UILabel
Post by: ArenMook on June 15, 2014, 06:15:17 AM
NGUIText.PrintCharacterPositions can be used to determine the positions of all characters. You can use it to position your sprite. Just don't forget to call label.UpdateNGUIText() prior to calling that function.
Title: Re: UILabel
Post by: Fractalbase on June 17, 2014, 06:37:33 PM
Is there a way to set vertical alignment?  I've implemented a work-around where the text variable has several '\n' characters inserted, but I'd prefer a better solution.
Title: Re: UILabel
Post by: xLeo on June 18, 2014, 09:18:27 AM
Is it possible or is it planned be implemented a way to use bbcode syntax to define effects (outline/shadow) and/or effect colors for a portion of the text?
Title: Re: UILabel
Post by: ArenMook on June 18, 2014, 08:23:17 PM
@Fractalbase: Yes, change the pivot to top or bottom. Bottom-left, Top-right, etc will also work.

@xLeo: No, no plans for that right now.
Title: Re: UILabel
Post by: Fractalbase on June 21, 2014, 06:47:01 AM
Are you sure regarding changing the pivot?  I've altered the pivot to be top, then bottom, but the text is always aligned at the center (vertically).
Title: Re: UILabel
Post by: ArenMook on June 21, 2014, 04:08:33 PM
Positive. Widget's pivot point is what changes the text's vertical alignment. Try it -- new scene, ALT+SHIFT+L, resize it so that it's large in size then change the widget's pivot to top.
Title: Re: UILabel
Post by: Fractalbase on June 21, 2014, 04:25:40 PM
I've having an issue with the Overflow setting.  The default is shrink content.  I want clamp content.  However, only 1 line of all of the lines in the text are shown.  While the game is running, if I select either of the resize options, the setting goes back to shrink content, but all of the lines of text are properly displayed.  Any ideas?
Title: Re: UILabel
Post by: Fractalbase on June 21, 2014, 04:43:06 PM
Regarding vertical alignment, I figured out the issue.  The Anchors of UILabel for "Bottom" and "Top" must be set to "Target's Bottom" and "Target's Top" (respectively) in order for the vertical alignment to work.
Title: Re: UILabel
Post by: ArenMook on June 21, 2014, 07:55:44 PM
If the widget is anchored on all sides, then it must assume the "Shrink" method. When the label's resize setting goes up against anchors, anchors win. Only one can adjust the height. Not both.

If you want to anchor the label and still have it auto-resize itself, then don't anchor both sides. For example, for Resize Height to work, anchor the Top side, but leave the Bottom not anchored.
Title: Re: UILabel
Post by: dkozlovtsev on July 04, 2014, 07:50:16 AM
NGUIText.PrintCharacterPositions can be used to determine the positions of all characters. You can use it to position your sprite. Just don't forget to call label.UpdateNGUIText() prior to calling that function.

UILabel.ApplyOffset is also needed to properly determine glyth position to place sprite on it, but it is protected, can it be made public? currently the only way is to change code or derive from UILabel and i would like to avoid both.
Title: Re: UILabel
Post by: ArenMook on July 04, 2014, 03:02:09 PM
Certainly.
Title: Re: UILabel
Post by: Tatanan on July 08, 2014, 06:47:00 AM
Where do I have to look to see NGUIText.ParseSymbol? Or all BBCode commands?
In particular, I need to make break lines.
Thank you.


BBcode style:

[b]bold[/b]
[i]italic[/i]
[u]underline[/u]
[s]strikethrough[/s]

...etc. Look inside NGUIText.ParseSymbol.
Title: Re: UILabel
Post by: Fractalbase on July 08, 2014, 07:03:29 AM
To break lines (make multiple lines), all you have to do is insert a carriage return character : '\n'
Title: Re: UILabel
Post by: ArenMook on July 08, 2014, 01:25:50 PM
@Tatanan: NGUIText.ParseSymbol is a function. NGUI comes with full source code -- you can view it when you need it. The full list is b, i, u, s, sub, sup, url, color tags (RrGgBb, RrGgBbAa), and alpha tags (Aa).
Title: Re: UILabel
Post by: Bypp on July 14, 2014, 09:26:13 AM
Hello ! Quick question concerning label text size. When I set a size for the text and set tit to shrink in case of overflow, is there a way to set the maximum size of my text ? I don't want a small amount of text to appear huge ! So I'd just set a maximum size for that case !

Is this possible ?
Title: Re: UILabel
Post by: ArenMook on July 15, 2014, 12:58:57 AM
The font size specified on the labels is the maximum font size. NGUI won't grow this value, only shrink it.
Title: Re: UILabel
Post by: Bypp on July 18, 2014, 10:36:31 AM
All I see is the relative size ! Thing is, I need to support many resolutions and that size depends (I'm also using fonts with different sized atlases, which might affect the final result), and setting that relative size to say 0.5 works well to some extent, but on a Kindle Fire device, the text will be considerable smaller, and on PC versions, the text will still grow pretty large unless I drastically reduce relative size. Is there a way to manually calculate that size depending on resolution ?
Title: Re: UILabel
Post by: ArenMook on July 19, 2014, 12:46:51 PM
Relative size? Are you using NGUI 2? There is no such thing in NGUI 3. You specify the font size on the labels themselves.
Title: Re: UILabel
Post by: Tulrath on September 03, 2014, 04:17:25 PM
Aren,

When I set the top or bottom gradient color from code, the color value doesn't seem to be getting set correctly.


...here is the code...


  1. UILabel label = GetComponent<UILabel>();
  2. label.gradientTop = new Color(255f,220f,0);
  3. label.gradientBottom = new Color(255f,0,0);
  4.  

...but when this runs, the color values seem to get "re-scaled" to a value between 0 and 65025, which results (apparently) in them not being considered correct colors, or just being ignored. 

At runtime if I then try to change the color values in the color gadget in the Unity Inspector to the proper RGB values, I get (in this example) a nice red-to-orange gradient as expected.  But when I set them through code, I get these "squared" values and the text just appears white.

Title: Re: UILabel
Post by: ArenMook on September 04, 2014, 11:09:40 AM
That's because Color values range from 0 to 1, not 0 to 255.

If you want to use 0 to 255, use Color32.
Title: Re: UILabel
Post by: Tulrath on September 06, 2014, 11:30:08 PM
That worked.  Thank you!
Title: Re: UILabel
Post by: Andresfdezb on September 18, 2014, 05:59:12 AM
Hi there,
I cant' figure out about the keep crisp option. I have a panel that holds a Scroll View with a Table inside. All table rows are basically UILabels, with some text. The problem comes whenever I resize the window, when some of the labels get blurry. My UIRoot is set to Flexible since I want my UI widgets to maintain it's size (which includes the labels), and all the labels have the keep crips set to always. Does this option only works for dynamic fonts? Because I'm using an embebbed font (arial) and labels get blurry when I resize the screen (and also text doesn't appear much crisp without even resizing).

Thanks in advance for the help.
Title: Re: UILabel
Post by: ArenMook on September 18, 2014, 05:26:38 PM
The dimensions of the window need to be dividable by two. Meaning if your window is something like 479x500, then the width part of is is not dividable by 2, resulting in a blurred UI.

And yes, the crisp option is only for dynamic fonts.
Title: Re: UILabel
Post by: Andresfdezb on September 22, 2014, 01:32:16 AM
The dimensions of the window need to be dividable by two. Meaning if your window is something like 479x500, then the width part of is is not dividable by 2, resulting in a blurred UI.

And yes, the crisp option is only for dynamic fonts.

Thanks, I'll have to run some more tests (and also look for a dynamic font, I guess).
Title: Re: UILabel
Post by: Andresfdezb on September 22, 2014, 02:28:01 AM
So, here's one last follow up question. I have imported one font from windows (ttf) and set it as dynamic. Is this setting correct? I mean, can ttf fonts be used as dynamic?

(Also I'm running some tests to check how well the crisp option works with different aspect ratios and screen sizes).
Title: Re: UILabel
Post by: ArenMook on September 22, 2014, 10:41:31 AM
TTF is TrueType Font. TTF and OTF files are both imported by Unity as Fonts, and yes you can use them as dynamic fonts for labels. Just keep in mind there are a lot of issues with dynamic fonts, and I don't advise using them. Stick to bitmap fonts by creating one via the NGUI's Font Maker.
Title: Re: UILabel
Post by: Andresfdezb on September 23, 2014, 01:19:17 AM
TTF is TrueType Font. TTF and OTF files are both imported by Unity as Fonts, and yes you can use them as dynamic fonts for labels. Just keep in mind there are a lot of issues with dynamic fonts, and I don't advise using them. Stick to bitmap fonts by creating one via the NGUI's Font Maker.

Ok, thanks.
Title: Re: UILabel
Post by: Maxii on September 26, 2014, 08:51:08 AM
From reading the different threads here, I was expecting MakePixelPerfect (from code) and the Snap Button to adjust the size of the label (widget dimensions) to fit the size of the font and Text currently present in the Text box. It doesn't seem to be doing the above, aka what I expect.

Can you give me a rundown of what I SHOULD expect, since obviously my current expectation is erroneous. I'm using the built in Arial Regular 12 (UIFont).

Does the Overflow setting make a difference in what to expect?
Title: Re: UILabel
Post by: ArenMook on September 27, 2014, 07:28:54 PM
Using that function will resize the label if its text currently doesn't fit. If the text fits, nothing will happen.
Title: Re: UILabel
Post by: bac9 on October 27, 2014, 09:34:07 AM
What would be a proper way to force UILabels to render dynamic fonts at a different resolution? Use of Flexible + Adjust by DPI UIRoot can lead the labels to believe they have to be rasterized at, say, 14px height, when in reality they occupy 28px on a physical screen (in case of 2x DPI adjustment). I take it I have to alter the arguments in RequestCharactersInTexture calls in NGUIText to do that? For example, by multiplying them by reliable static multiplier that will correct the rasterized resolution to proper one if UIRoot is scaled by DPI adjustment.

There is quite a lot of these calls, through, and I'm not seeing immediate changes to resolution after altering finalSize arguments, so I might be wrong.

P.S.: I know it would've been easier with bitmaps, but I'm afraid I don't have an option of using them (full Unicode support multiplied by reasonably big set of sizes and thicknesses requested by guidelines multiplied by six screen densities would produce quite a mess with bitmaps, so I think dynamic font labels win in convenience in my case).
Title: Re: UILabel
Post by: bac9 on October 27, 2014, 10:10:16 AM
I think I almost got it, just need to fix alignment issues now. I changed:

Line 235 in NGUIText.GetGlyph
Quote
if (dynamicFont.GetCharacterInfo ((char) ch, out mTempChar, Mathf.RoundToInt (finalSize * screenMultiplier), fontStyle))

Line 258 in NGUIText.GetGlyph
Quote
float pd = fontScale * pixelDensity / screenMultiplier;

(http://i.imgur.com/wgnCAO7.jpg)

I think I need to alter Update in the beginning, but adding multiplier into baseline variable is not entirely fixing it, so I might be missing someplace else.

_________________

Edit: Still a bit wrong, but it's very close. Am I altering the right place in this baseline calculation?

Quote
float y0 = mTempChar.vert.yMax;
float y1 = mTempChar.vert.yMin;
baseline = Mathf.Round (y0 + (finalSize - y0 + y1) * 0.5f * screenMultiplier);

(http://i.imgur.com/z53qnE5.jpg)

Both y0 and y1 are not changed when GetGlyph encounters non-1 screenMultiplier, so it's unlikely I need to multiply those by anything. Multiplying finalSize doesn't look right either, as well as multiplying the whole thing before rounding. I'm probably overlooking something.

_________________

Edit 2: Or maybe baseline is alright all along and I should only alter something else in the GetGlyph method. For example, adding this comes extremely close to the desired result:

Line 246 in NGUIText.GetGlyph
Quote
glyph.v0.y = (mTempChar.vert.yMax - baseline) * screenMultiplier;

(http://i.imgur.com/qpdYZNW.jpg)

Except the result seems to deviate undesirably between glyphs, breaking proper vertical alignment. Ugh. :)

_________________

Edit 3: Ugh, so close yet wrong again!

Line 246 in NGUIText.GetGlyph
Quote
glyph.v0.y = (mTempChar.vert.yMax - baseline) * screenMultiplier;
glyph.v1.y = (glyph.v0.y - mTempChar.vert.height) / screenMultiplier;

(http://i.imgur.com/xLZp9SS.jpg)

Letter U no longer sinks, but now O fails to raise above other quads like it does in properly aligned x1 scaling. What the hell am I missing?

_________________

Edit 4: Okay, I'm starting to have a suspicion I have missed an already present pixel density correction that's right under my nose. Never saw the existing pixel density parameter in NGUIText be anything other than 1, but figuring out where it's assigned might be the right way to approach the issue instead of tinkering with delicate stuff in NGUIText.

_________________

Edit 5: Christ, that's completely right!

(http://i.imgur.com/XiT8tod.jpg)

I forgot that I bypassed the way standard UIRoot DPI adjustment works, which is why labels never received any notice about changed density, - as the property of UIRoot they were reading was not changed by screen density logic anymore. Very simple fix.

Line 1792 in UILabel
Quote
if (rt != null) NGUIText.pixelDensity = (rt != null) ? 1f / screenMultiplier : 1f;
Title: Re: UILabel
Post by: meowikanz on November 04, 2014, 05:58:58 PM
hi, i have looked at the reference for uilabel.alignment but i cant find out how to change it in code....

(UIlabel).alignment = ? i can work out how to set it, sometimes its really confusing for me to find examples of how to change things in code, its always clear as crystal via the unity environment.
Maybe im not looking in the right places :(

thanks for any help! jus a simple example of changing a uilabel's alignment to left upper would be really appreciated.

Title: Re: UILabel
Post by: meowikanz on November 04, 2014, 06:06:05 PM
ok i worked it out sorry i used the search function for the forum will do that from now on.
in case anyone wants to know its UIlabel.alignment = NGUIText.Alignment.(pick ur side)
Title: Re: UILabel
Post by: PoN on December 04, 2014, 09:50:55 PM
Why i can't set Overflow of UILabel to - "Resize freely" , when i set anchor at the label. if anchors are off , it can be set to Resize freely.

What's wrong ?

thanks.
Title: Re: UILabel
Post by: ArenMook on December 05, 2014, 01:06:35 AM
Because you are anchoring all sides. How can it resize freely if you are anchoring all sides?

Try using advanced anchoring, and set only 2 sides, such as Top and Left.
Title: Re: UILabel
Post by: sstublic on December 15, 2014, 12:03:08 PM
Hi!

I have the following problem: I'm using UILabel to display text (which is dynamic, meaning I don't know exact contents at compile time) which is in blue gradient.
However, some of the words in the text need to be able to become orange gradient (as in highlighted).

Basically I could do this by making the gradient light gray to dark gray and then use UILabel color (via color escape codes) to tint that gradient either blue or orange.
HOWEVER, this solution doesn't work for me, because color gradients I'm using CANNOT be reproduced as a combination of gray gradient and tint.

Could you point me in any meaningful direction how to do this either by:
a) extending color coding functionality to allow gradient escape codes (as in for example [RrGgBb, RrGgBb])
b) somehow preprocessing the text myself before hand and then programatically generate 'concatenated' multiple labels, each for the same colored segment

I've been looking at UILabel code and NGUIText code, but I'd like a way to this without modifying your code, or at least without modifying a lot of it.

Could something like this be done via UILabel inheritance? Do you have any other ideas how to do this?

Thanks!

Title: Re: UILabel
Post by: ArenMook on December 30, 2014, 01:57:36 PM
Preprocessing is trivial. When you are about to set the label's text, do your parsing that will determine which gradient should be used and set the label's gradientTop and gradientBottom accordingly. There is no need to modify NGUI for this.
Title: Re: UILabel
Post by: sstublic on January 17, 2015, 06:30:12 AM
Since I needed some of the words having different gradient in the same label (as explained above), that approach wouldn't work at all.

I ended up modifying NGUIText.cs and inserting it to call custom function whenever it's determining final color for each letter using its tint and gradient (in my case either top or bottom). Something like:

  1.  public static Color CombineTintAndGradient(Color tint, Color gradient)

This allowed me to write custom logic on how gradients are formed and fixed my problem. It is a hack, but worked.
If anyone runs into similar problems, feel free to msg me for details.
Title: Re: UILabel
Post by: Nicki on February 13, 2015, 09:27:40 AM
I would love to have a bbcode-like tag like so:

  1. label.text = "I'm a text with [size=+10] big parts[/size] and [size=-10] small parts[/size] and a [size=14]absolute sized part[/size]"
  2.  

To generate a single label with multiple sizes

I'm a text with big parts and small parts and an absolute sized part.

Since designers tend to want that and right now I have to create multiple UILabels and align them and all sorts of shenanigans to fill out each individual label with data.
Title: Re: UILabel
Post by: helmesjo on March 17, 2015, 07:00:00 AM
I would love to have a bbcode-like tag like so:

  1. label.text = "I'm a text with [size=+10] big parts[/size] and [size=-10] small parts[/size] and a [size=14]absolute sized part[/size]"
  2.  

To generate a single label with multiple sizes

I'm a text with big parts and small parts and an absolute sized part.

Since designers tend to want that and right now I have to create multiple UILabels and align them and all sorts of shenanigans to fill out each individual label with data.

I second this. Designers sure can get sad when you deny them this... :)
Title: Re: UILabel
Post by: Nicki on March 19, 2015, 05:11:15 PM
I've actually made a version of this in the project I'm working on now, but it has some dodgy bits so I haven't been able to push it back to NGUI. The issue comes from how it calculates width and height - width can be solved because you can add each glyph one at a time like you do now, but figuring out the height of a line is difficult, because you have to parse through the whole line before you can reliably place the first character - that means you have to run through each line twice to place the vertical placement properly and that sorta sucks.

That said, as long as you work on a single line it can work. I do it with [size=0.5] where the size is relative to the label's default size. If I can find time to clean it up, I'll push this back to NGUI in a pull request.

I second this. Designers sure can get sad when you deny them this... :)
Title: Re: UILabel
Post by: Tatanan on April 15, 2015, 09:26:52 AM
What's the difference between Outline and Outline8?
Title: Re: UILabel
Post by: ArenMook on April 16, 2015, 07:43:19 AM
Outline draws the outline by drawing text 4 extra times. Outline8 draws it 8 extra times
Title: Re: UILabel
Post by: Tatanan on April 16, 2015, 07:52:54 AM
I guess I should just use outline "4", right?
Title: Re: UILabel
Post by: ArenMook on April 16, 2015, 08:25:19 AM
In most cases, yup.
Title: Re: UILabel
Post by: dadrester on July 30, 2015, 07:57:42 AM
What are the chances of getting functionality for changing font within a label? (if it's within the same atlas). I'm assuming they're low :)
Title: Re: UILabel
Post by: ArenMook on July 31, 2015, 10:11:50 PM
Yeah it complicates the label's code drastically. I personally don't want to delve into that as I know it will cause more harm (read: bugs) than good. With the ability to change colors, italic / bold / underline and have hyperlinks / emoticons in text, you can already customize it quite a bit. The ability to change fonts halfway through the sentence is a niche feature.
Title: Re: UILabel
Post by: blitzer on December 30, 2015, 08:18:09 AM
I've got a question about creating multiple line breaks through code, say if I wanted something like:
Some Text
[blank line]
More Text

The only way I've found to accomplish this is to do:
  1.         void SomeMethod()
  2.         {
  3.                 //show popup that lets the user know we are validating their internet connection:
  4.                 failureString = "Could not verify internet connection, make sure you have access to a network and are connected to it. Please wait 10 seconds after fixing your connection before trying again.@@(Tap to Clear)";
  5.                 failureString = failureString.Replace ("@", "\n");
  6.  
  7.                 //or could use: failureString = "Could not verify internet connection, make sure you have access to a network and are connected to it. Please wait 10 seconds after fixing your connection before trying again." + "\n" + "\n" + "(Tap to Clear)";
  8.  
  9.                 label.text = failureString;
  10.         }

Where the text for the string is added later in the script after the declaration of the string. Declaring the string with the text, then running Replace() doesn't work (and running Replace() on label.text also has issues), and everything else I've tried results in:
Some Text
More Text

Any ideas are appreciated.
Title: Re: UILabel
Post by: ArenMook on January 01, 2016, 07:24:07 PM
Why are you doing the replace at all? Just have "\n" there to begin with. It's always like that with text.
  1. failureString = "Could not verify internet connection, make sure you have access to a network and are connected to it. Please wait 10 seconds after fixing your connection before trying again.\n\n(Tap to Clear)";
Title: Re: UILabel
Post by: blitzer on January 02, 2016, 06:25:23 AM
"Why are you doing the replace at all? Just have "\n" there to begin with. It's always like that with text."

I could swear I tried that (since as you say, this is a common behavior). But I did try it again and it worked, really don't know what I was doing wrong when I was trying this before but everything is fine now. Thank you for your time.
Title: Re: UILabel
Post by: skatola on February 15, 2016, 10:31:37 PM
Can i change font in runtime in the same string?
for example:

[times new roman] "hello" + [arial black] "world"
Title: Re: UILabel
Post by: ArenMook on February 18, 2016, 07:10:48 PM
No, you can't change fonts mid-label.
Title: Re: UILabel
Post by: Argiris on March 29, 2016, 05:47:49 AM
Hello.

We've got a curious issue in our game, and I thought maybe some of you may have an idea about it.

Our game supports XHDPI and XXHDPI devices and fonts appear fine in development mode, however in production it seems like we're getting the XHDPI in XXHDPI devices (in other words, all fonts are blurry in high resolution screens).

Has anyone seen something like that before?

Thank you in advance!