Thread Tools Display Modes
Prev Previous Post   Next Post Next
08/16/14, 06:29 AM   #1
circonian
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 613
Set Width to Text Length

What I'm trying to do:
I'm creating a variable sized window, with a variable amount of buttons inside of it, which have text of varied lengths. I was just curious if there is an easy way to get the width of the text inside of a button. So that if the width of the text changes, due to font/size changes the button text changes, I can reset the width of ALL of the buttons.

Right now I'm just grabbing the string length of every buttons text & 'roughly estimating' the width needed for the buttons, but it seemed like there should be an easier way for something like this.

If it helps, this is what I'm doing:
Lua Code:
  1. -- Create the parent window --
  2. local function CreateQuickClickWindow()
  3.     if not Click4Info.QuickClickWindow then
  4.         Click4Info.QuickClickWindow = WINDOW_MANAGER:CreateTopLevelWindow("Click4InfoQuickClickWindow")
  5.         local qcWin = Click4Info.QuickClickWindow
  6.         qcWin:SetDimensions(windowWidth, ((#QuickClickCommands+1) * Click4Info.QuickClickButtonHeight))
  7.         qcWin:ClearAnchors()
  8.         qcWin:SetAnchor(TOPLEFT, GuiRoot, TOPLEFT, Click4Info.QuickClickOffsetX, Click4Info.QuickClickOffsetY)
  9.         qcWin:SetHidden(false)    
  10.         qcWin:SetMovable(true)
  11.         qcWin:SetMouseEnabled(true)
  12.         qcWin:SetClampedToScreen(true)    
  13.  
  14.         qcWinBG = WINDOW_MANAGER:CreateControlFromVirtual("Click4InfoQuickClickWindowBG", qcWin, "ZO_DefaultBackdrop")
  15.         qcWinBG:ClearAnchors()
  16.         qcWinBG:SetAnchorFill(qcWin)
  17.     end
  18. end

and create buttons like this:
Lua Code:
  1. -- Create the Buttons --
  2. local function CreateQuickClickButtons()
  3.     for i = 1, #QuickClickCommands do
  4.         local button = CreateControlFromVirtual("Click4InfoQuickClickButton", Click4Info.QuickClickWindow, "ZO_BladeHeader", i)
  5.         button:SetHidden(false)    
  6.         button:ClearAnchors()
  7.         button:SetDimensions(windowWidth, Click4Info.QuickClickButtonHeight)
  8.         button:SetAnchor(TOPLEFT, Click4Info.QuickClickWindow, TOPLEFT, 0, Click4Info.QuickClickButtonHeight * (i-1))
  9.         button:SetText("    "..QuickClickCommands[i])
  10.         if i == 1 then
  11.             button:SetHandler("OnClicked", function() Click4Info.freeMouse = (not Click4Info.freeMouse) end)
  12.         else
  13.             button:SetHandler("OnClicked", function() QuickClickBtnClick(i) end)
  14.         end
  15.         button:SetHorizontalAlignment(TEXT_ALIGN_LEFT)
  16.     end
  17. end

XML:
xml Code:
  1. <GuiXml>
  2.     <Controls>
  3.         <Button name="MY_BladeHeader" font="ZoFontGame" endCapWidth="22" verticalAlignment="LEFT" virtual="true">
  4.        
  5.             <Dimensions x="200" y="28"/>
  6.             <PressedOffset x="2" y="2" />
  7.  
  8.             <OnInitialized>
  9.                 ZO_ToggleButton_Initialize(self, TOGGLE_BUTTON_TYPE_BLADE)
  10.             </OnInitialized>
  11.            
  12.             <OnClicked>
  13.                 ZO_CheckButton_OnClicked(self, button)
  14.             </OnClicked>
  15.                    
  16.             <FontColors
  17.                 normalColor="INTERFACE_COLOR_TYPE_TEXT_COLORS:INTERFACE_TEXT_COLOR_NORMAL"
  18.                 pressedColor="INTERFACE_COLOR_TYPE_TEXT_COLORS:INTERFACE_TEXT_COLOR_SELECTED"
  19.                 mouseOverColor="INTERFACE_COLOR_TYPE_TEXT_COLORS:INTERFACE_TEXT_COLOR_HIGHLIGHT"
  20.                 disabledColor="INTERFACE_COLOR_TYPE_TEXT_COLORS:INTERFACE_TEXT_COLOR_DISABLED"
  21.             />
  22.         </Button>
  23.                
  24.     </Controls>
  25. </GuiXml>

Last edited by circonian : 08/16/14 at 07:50 AM.
  Reply With Quote
 

ESOUI » Developer Discussions » Lua/XML Help » Set Width to Text Length

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off