Author Topic: UILabel's font size not honouring pixel size in 3.4.9  (Read 12982 times)

davidoakley

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
UILabel's font size not honouring pixel size in 3.4.9
« on: February 19, 2014, 06:20:40 AM »
Hi,

I'm using reference fonts/atlases to support pixel densities on mobile devices. Since the introduction of the pixel size property on UILabel I've found that when switching pixel sizes labels jump to double or half their intended font size.

What I'm seeing is that the font size (as seen on the UILabel inspector) is 43 when using the SD (1.0 pixel size) version of a font, but 85 when using HD (0.5 pixel size). That makes sense in terms of real font pixels, but surely the UILabel should work in terms of (font size * atlas pixel size)? Whichever value I fix in the inspector is wrong for the other pixel density screen.

If I force label.fontSize = font.defaultSize then it fixes it OK, but that means having code everywhere to fix things up.

I've checked and the fonts' pixelSizes are correctly picking up the pixelSizes from their parent atlases.

Is there anything I've missed? Normally I try and fix these things myself, but I'm not sure where the right place is to start...

Many thanks,
David

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UILabel's font size not honouring pixel size in 3.4.9
« Reply #1 on: February 19, 2014, 12:46:17 PM »
Let's revisit this in 3.5.0. There were some changes related to this quite some time ago.

Fault

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: UILabel's font size not honouring pixel size in 3.4.9
« Reply #2 on: February 20, 2014, 09:09:51 AM »
I'm experiencing the same issue using NGUI 3.5.0.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UILabel's font size not honouring pixel size in 3.4.9
« Reply #3 on: February 20, 2014, 09:16:01 AM »
Alright, how would I repro this so I better understand what you mean? Doing a quick test reveals everything working as expected -- bitmap font, change its pixel size, labels get adjusted accordingly. A label with a bitmap font set to pixel size of 0.5 and label font size of 96 has the same visual size as a bitmap font set to pixel size of 1 and label font size of 48.

davidoakley

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: UILabel's font size not honouring pixel size in 3.4.9
« Reply #4 on: February 21, 2014, 07:24:18 AM »
Hi, thanks for the reply!

My problem happens when switching between NGUI fonts using a reference font.

In your example:

I have Font_SD which is 48 pixels high with pixel size 1.0.
I have Font_HD which is 96 pixels high with pixel size 0.5.
I have a reference font, FontRef, pointing to Font_SD.
I have a label with font size 48 displaying FontRef.

Initially the label shows the 48 pixel-high font at 48 at the correct size.

Now running on a high density device with a UI Root height set to half the real pixel height, I switch FontRef to point to Font_HD.
The label is now showing a 96 pixel high font at size 48, so it shows at 50% of its original size. This causes it to show on-screen at 48 real pixels high rather than the 96 (48 / 0.5) that it should.

Hope that makes sense! If not I can create a small test scene...

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UILabel's font size not honouring pixel size in 3.4.9
« Reply #5 on: February 21, 2014, 02:38:53 PM »
But that's what it's supposed to be... If the label uses font size of 48 pixels, then that's exactly what it's going to be, regardless of the font pixel size. It should remain consistent size because UIRoot also remains of consistent size.

Fault

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: UILabel's font size not honouring pixel size in 3.4.9
« Reply #6 on: February 22, 2014, 01:39:41 PM »
In my case I have Atlas_SD with pixel size 1.0 and Atlas_HD with pixel size 0.5. Then I have a Font_SD which is stored in Atlas_SD and a Font_HD stored in Atlas_HD.

The UIRoot is set to a specific height.

If I create a label using Font_SD and then switch it to Font_HD it should visibly remain the same size right? Just like sprites do when you switch from using the atlas_sd to atlas_hd right? Well that's not what happens, it will visibly have half the size compared to using Font_SD.

This worked fine before I upgraded NGUI from 2.x to 3.x.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UILabel's font size not honouring pixel size in 3.4.9
« Reply #7 on: February 23, 2014, 10:56:41 AM »
Can you send me a repro case to have a look at? (support at tasharen.com, dropbox download link works best)

davidoakley

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: UILabel's font size not honouring pixel size in 3.4.9
« Reply #8 on: February 24, 2014, 05:58:20 AM »
I've created a test case project, which (predictably?) shows that the system does work as expected, as long as you set the label font size when pixel size is 1.0.

I think that the catch is that the "Default" font size on a label isn't taking the pixel size into account, so in HD (pixel size 0.5), the correct values for the label in my example is:

Font Size: 28 Default: 59

That shows the font pixel perfectly. If you trust the default value and set the label's font size to 59 then it doubles in size.

I'm wondering if it is that prefabs that were created before labels had font sizes get set to their default size on upgrade (which will be at runtime), which is wrong if pixel size <> 1.0? That would make sense. I guess opening each prefab in the editor and applying (when pixel size is 1.0) would do the trick, but I wonder if that upgrade script needs fixing? Sorry I haven't got time right now to get an old NGUI to try that...

David

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UILabel's font size not honouring pixel size in 3.4.9
« Reply #9 on: February 24, 2014, 04:28:20 PM »
Hmm... replace UIFont.defaultSize with this:
  1.         public int defaultSize
  2.         {
  3.                 get
  4.                 {
  5.                         if (mReplacement != null) return mReplacement.defaultSize;
  6.                         if (isDynamic) return mDynamicFontSize;
  7.                         return Mathf.RoundToInt(mFont.charSize * pixelSize);
  8.                 }
  9.                 set
  10.                 {
  11.                         if (mReplacement != null) mReplacement.defaultSize = value;
  12.                         else mDynamicFontSize = value;
  13.                 }
  14.         }

Fault

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: UILabel's font size not honouring pixel size in 3.4.9
« Reply #10 on: February 25, 2014, 12:58:19 PM »
That fix worked somewhat, the label's size doesn't change now, but there's now another issue. With SD font it looks correct, but when I change to HD now it gets offset down a bit. I'm using shrink content, but if I change it to resize freely and use the HD font the widget container rect will be half the height of the label.

I've attached some pictures to demonstrate what I'm talking about.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UILabel's font size not honouring pixel size in 3.4.9
« Reply #11 on: February 25, 2014, 01:07:32 PM »
That would be because pixel size gets applied twice.
  1. NGUIText.fontScale = isDynamic ? mScale : ((float)mFontSize / mFont.defaultSize) * mScale * bitmapFont.pixelSize;
should be:
  1. NGUIText.fontScale = isDynamic ? mScale : ((float)mFontSize / mFont.defaultSize) * mScale;
It needs to be changed in two places in UILabel. Lines 1086 and 1632 in my case.

davidoakley

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: UILabel's font size not honouring pixel size in 3.4.9
« Reply #12 on: February 26, 2014, 05:15:17 AM »
Thanks @Fault, I was seeing that too but hadn't got round to describing the problem...

@ArenMook, that fix doesn't seem to do the trick. Here's a before / after screenshot from our game:



All the text is now being scaled by 1 / pixelSize. Seems like the it's just NGUIText.finalLineHeight that needs fixing, as dividing that by 0.5f in NGUIText.Update() fixes the alignment and multi-line problems. But how to affect that without affecting the font scale...?

Hidden

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: UILabel's font size not honouring pixel size in 3.4.9
« Reply #13 on: February 26, 2014, 08:42:25 AM »
I'm having a similar issue with 3.5.1.

I have an HD atlas with pixel scale of 0.5 and an SD atlas with pixel scale 1, both contain 2 fonts generated in GlyphDesigner, the SD atlas contains the font exported at size 15, and HD atlas contains fonts exported at size 30 but named 15. In previous NGUI versions this was fine, I could switch between HD and SD fine (there would be tiny kerning changes that caused no problems), but now with 3.5.1 the HD font looks tiny.

I copied in the code snippets above but that seems to have broken the font even more as now its showing a different part of the atlas.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UILabel's font size not honouring pixel size in 3.4.9
« Reply #14 on: February 26, 2014, 02:07:20 PM »
I'll look into this in greater detail shortly.