08/16/14, 06:29 AM | #1 |
|
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:
and create buttons like this: Lua Code:
XML: xml Code:
Last edited by circonian : 08/16/14 at 07:50 AM. |
08/16/14, 07:28 AM | #2 |
|
I have not tried this, just thinking. It depends on the button template you use.
If it's a <Control> with the text as a child <Label>, you could try setting resizeToFitDescendents="true" on the parent, and resize all labels to the width of the widest. If it's a <Button>, it should have GetDesiredWidth methods. From the name I'd expect GetDesiredWidth to give me how wide the button wants to be to fit the text, then I'd set all buttons' width to the maximum value. Or if that fails, GetLabelControl and use the first method on that. edit: just noticed the template is <Button name="ZO_BladeHeader" ... nvm I'll go try that Last edited by merlight : 08/16/14 at 07:30 AM. |
08/16/14, 07:51 AM | #3 | |
|
I tried the desired width. Even if I take the SetDimensions out and the dimension tag from the xml it comes back with a desired width of 200 regardless of the text inside the button. P.S. I've seen people do posts where they had code hidden & you had to click a button to view it...how do you do that, anyone know? |
|
08/16/14, 08:32 AM | #4 | ||
|
I tried GetLabelControl:GetTextWidth(). I created two buttons under FPS window, and a "/qlick index text" slash command to change the text. They seem to resize correctly. Another option would be to anchor TOPRIGHT of each button to the previous button's BOTTOMRIGHT, and then only resize the first button.
Warning: Spoiler
Lua Code:
|
||
08/17/14, 06:18 AM | #5 | |
|
I think I narrowed it down. (in the above discussion we mentioned that) We found out if we did not set a button dimension it defaulted to a width of 200...and it seems that the GetLabelControl():GetTextWidth() only works if the text actually fits in the button (so without a dimension that constrains it to a text width < 200). I even tried copy & pasting your work, and if button text is longer than 200 it seems to not work when loading up. Even if I run the resizer manually after the game loads (through /znb) I have to run it 3-4 times before the buttons get to the proper size. It appears that even the GetTextWidth() is limited by the button size? ...if any of that makes any sense to you. I removed the calls on initialize & ran the create buttons after the game loaded to see what values the GetTextWidth() function was getting when the function ran, heres what the code looked like:
Warning: Spoiler
Lua Code:
and heres the output: Which would be why the resize isn't working when it starts up. Those are not the correct text widths. Since you added +40 to the text width in your resize function is the only reason resize worked (after 3-4 times) because it kept increasing the button width by 40 until it got to where it was supposed to be. If I remove the +40 I can run the resize over & over and the width of the buttons never change. |
|
08/17/14, 07:43 AM | #6 |
|
I added +40 as padding (using SetNormalOffsets(20, 0) I set left padding to 20; so another 20 for the right). Now I found another text width function, try this:
Lua Code:
|
08/17/14, 04:40 PM | #7 | |
|
Lua Code:
Thanks for your help. |
|
08/17/14, 05:27 PM | #8 |
|
|
08/18/14, 05:47 AM | #9 |
|
I'm glad you responded...and you were right. I don't know what happened last time, but I tried it again & it worked this time. Now I know how to handle it for later cases..thank you very much for the help.
|
ESOUI » Developer Discussions » Lua/XML Help » Set Width to Text Length |
«
Previous Thread
|
Next Thread
»
|
Display Modes |
Linear Mode |
Switch to Hybrid Mode |
Switch to Threaded Mode |
|
|