Author Topic: Best way to size UILabel widths for different devices  (Read 3684 times)

GavinF

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 16
    • View Profile
Best way to size UILabel widths for different devices
« on: July 24, 2013, 09:53:48 PM »
I have been struggling with this for a while now.  I have a UILabel that needs to take up a certain percentage of my screen.  My designer is a stickler and wants the text to have margins that are uniform from device to device.  To clarify, when we are using a wider screen device, I need to make the UILabel.lineWidth larger.  We built our UI using a 320x480 layout (iPhone Tall 320x380 in Unity Editor game window).  What I have been doing (and what works on most, but not all devices) is take the current lineWidth of 190 (which is the correct size for the 320x480 layout), and multiply it by a ratio of the difference in screen sizes and multiply by the pixel scale.  So, something like this:

newWidth = oldWidth * Screen.width / 320.0f * UIRoot.GetPixelSizeAdjustment(Screen.height); 

I then also adjust the local position of my UILabels parent GameObject by half of the difference between the old and new widths.  My problem is that this does not work on all devices.  On an iPad3, I end up with a width of 213, where I was expecting a 225.  My designer mentioned that it might be pixel density that is throwing things off, but I'm wondering if aspect ratio needs to be factored in somehow.  Am I missing a magic number somewhere?  Any advice appreciated.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Best way to size UILabel widths for different devices
« Reply #1 on: July 25, 2013, 03:49:39 AM »
Density has nothing to do with it. My guess -- you have Screen.width and height flipped (in tall 320x480 mode, width is 320, height is 480).

GavinF

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 16
    • View Profile
Re: Best way to size UILabel widths for different devices
« Reply #2 on: July 25, 2013, 11:46:44 AM »
Our app is always in portrait (tall) mode.  That said, does the formula above look correct, or are you saying I should be passing Screen.width into GetPixelSizeAdjustment? iPad Tall (3:4) is the only resolution causing a real problem so far.  Maybe I'll just hard-code it for that device =/
« Last Edit: July 25, 2013, 01:16:34 PM by GavinF »