View Single Post
09/18/15, 08:32 PM   #7
circonian
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 613
If the tabControl has a scale applied to it then it also scales the text by that amount.
So as an example, if you have a label with a scale of 1.5 and you put text in it that has an actual width of 100 (with a scale of 1.0), the text will get scaled up to 100 * 1.5 = 150.

But the real problem looks like the GetStringWidth() function also applies the scale to the returned value:
Lua Code:
  1. -- lets say the text width is actually 100 (with a scale of 1.0)
  2. -- and you set the label scale to 1.5 and you do:
  3. local width = label:GetStringWidth(label:GetText())
  4.  
  5. -- the width will be 100 * 1.5 * 1.5 = 225

It actually returns the actual text width * 1.5 * 1.5. The first multiplier is from the label being scaled which also scaled the string. The second multiplier is coming from the GetStringWidth() function applying another 1.5 multiplier to the returned width. Since the text width was already scaled I don't know why it would do that, but that is what it looks like its doing.

So you need to get the string width before you scale the control or adjust the width after you get it.
Lua Code:
  1. -- with a scale already applied
  2. tabControl:SetText(title)
  3.  
  4. local textWidth = tabControl:GetStringWidth(title)
  5. local scale = tabControl:GetScale()
  6.  
  7. -- get real text width on a scale of 1.0
  8. textWidth = textWidth / (scale * scale)
  9.  
  10. tabControl:SetWidth(10 + textWidth)

EDIT: I forgot to add, the reason you want the real text width (on a scale of 1.0) is because when you use SetWidth(width) it applies the scale to the width that you pass into it. So if the label has a scale of 1.5 and you do
Lua Code:
  1. label:SetWidth(100)
the width will actually get set to 150

Last edited by circonian : 09/18/15 at 08:51 PM.
  Reply With Quote