WoWInterface

WoWInterface (http://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (http://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   resize frame to fit text (http://www.wowinterface.com/forums/showthread.php?t=46685)

Spawnova 06-18-13 06:03 PM

resize frame to fit text
 
My frame has a lot of text that changes and often it will extend beyond the frame, how would I go about setting the frame width correctly fit the text when it changes?

Nibelheim 06-18-13 06:06 PM

Quote:

Originally Posted by Spawnova (Post 279942)
My frame has a lot of text that changes and often it will extend beyond the frame, how would I go about setting the frame width correctly fit the text when it changes?

When you set the FontString's text, use :GetStringWidth() on your FontString, this will tell you how wide the string is and you can set the width of your Frame accordingly.

Spawnova 06-18-13 06:20 PM

Perfect, thanks!

semlar 06-18-13 07:40 PM

You could just set the frame's points relative to the font string's like frame:SetPoint('TOPRIGHT', fontstring, 4, 4) and that will automatically size the frame based on the fontstring.

Phanx 06-19-13 02:29 AM

That won't work if the font string won't work if the font string is anchored to the frame -- you'll get an error about circular references.

SDPhantom 06-22-13 12:20 AM

The way to make it work by anchoring is to create the frame first, then the fontstring, both with anchors not set yet. Instead of the conventional anchor fontstring to frame and frame to parent, you do the reverse. Anchor the frame to the fontstring and the fontstring to the frame's parent.

Lua Code:
  1. local Frame=CreateFrame("Frame",nil,UIParent);
  2. local Text=Frame:CreateFontString(nil,"OVERLAY","GameFontNormal");
  3.  
  4. Frame:SetAllPoints(Text);-- Set points for frame
  5. Text:SetPoint("CENTER",UIParent,"CENTER",0,0);--    Move text to location

humfras 06-22-13 02:12 AM

For me, hooking the fontstring's SetText function would be the easiest way because you wouldn't need to change anchors.

e.g.
hooksecurefunc(fontstring, "SetText", function(self) self:GetParent():SetWidth(self:GetWidth()) end)

Seerah 06-22-13 11:30 AM

Using the anchors is cheaper than hooking and using a function (that calls 3 other functions), though. The anchors are the more "elegant" method.

humfras 06-23-13 03:17 AM

Quote:

Originally Posted by Seerah (Post 280170)
Using the anchors is cheaper than hooking and using a function (that calls 3 other functions), though. The anchors are the more "elegant" method.

The question is: How often will the text change? How's the frames' structure?
We don't know nothing about^^

Lombra 06-23-13 11:46 AM

Does anchoring really work? The font string object is never automatically resized to fit the text, is it? I'd go with the GetStringWidth approach.

semlar 06-23-13 04:57 PM

Yes, anchoring really works. It's how I normally handle this.


All times are GMT -6. The time now is 05:32 AM.

vBulletin © 2017, Jelsoft Enterprises Ltd
© 2004 - 2017 MMOUI