Reply
Thread Tools Display Modes
Unread 06-18-13, 06:03 PM   #1
Spawnova
A Black Drake
 
Spawnova's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2012
Posts: 87
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?
Spawnova is offline   Reply With Quote
Unread 06-18-13, 06:06 PM   #2
Nibelheim
local roygbi-
 
Nibelheim's Avatar
Featured
Join Date: Jan 2010
Posts: 1,539
Originally Posted by Spawnova View Post
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.
Nibelheim is offline   Reply With Quote
Unread 06-18-13, 06:20 PM   #3
Spawnova
A Black Drake
 
Spawnova's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2012
Posts: 87
Perfect, thanks!
Spawnova is offline   Reply With Quote
Unread 06-18-13, 07:40 PM   #4
semlar
A Molten Giant
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 600
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.
semlar is offline   Reply With Quote
Unread 06-19-13, 02:29 AM   #5
Phanx
A Pyroguard Emberseer
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 4,391
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.
__________________
Author/maintainer of Grid, PhanxChat, ShieldsUp, and many more.
Troubleshoot an addonTurn any code into an addonMore addon resources
Need help with your code? Post all of your actual code! Attach or paste your files.
Please don’t PM me about addon bugs or code questions. Post a comment or forum thread instead!
Phanx is offline   Reply With Quote
Unread 06-22-13, 12:20 AM   #6
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 1,026
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
__________________
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
SDPhantom is online now   Reply With Quote
Unread 06-22-13, 02:12 AM   #7
humfras
A Warpwood Thunder Caller
AddOn Author - Click to view addons
Join Date: Oct 2009
Posts: 97
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)
__________________
Author of VuhDo CursorCastBar OptiTaunt Poisoner RaidMobMarker
humfras is offline   Reply With Quote
Unread 06-22-13, 11:30 AM   #8
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 9,624
Using the anchors is cheaper than hooking and using a function (that calls 3 other functions), though. The anchors are the more "elegant" method.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

Seerah is online now   Reply With Quote
Unread 06-23-13, 03:17 AM   #9
humfras
A Warpwood Thunder Caller
AddOn Author - Click to view addons
Join Date: Oct 2009
Posts: 97
Originally Posted by Seerah View Post
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^^
__________________
Author of VuhDo CursorCastBar OptiTaunt Poisoner RaidMobMarker
humfras is offline   Reply With Quote
Unread 06-23-13, 11:46 AM   #10
Lombra
A Cobalt Mageweaver
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 235
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.
__________________
Grab your sword and fight the Horde!
Lombra is offline   Reply With Quote
Unread 06-23-13, 04:57 PM   #11
semlar
A Molten Giant
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 600
Yes, anchoring really works. It's how I normally handle this.
semlar is offline   Reply With Quote
Reply

Go BackWoWInterface » Developer Discussions » Lua/XML Help » resize frame to fit text

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