Thread Tools Display Modes
06-18-13, 06:03 PM   #1
Spawnova
A Warpwood Thunder Caller
 
Spawnova's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2012
Posts: 96
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?
  Reply With Quote
06-18-13, 06:06 PM   #2
Nibelheim
local roygbi-
 
Nibelheim's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 1,600
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.
  Reply With Quote
06-18-13, 06:20 PM   #3
Spawnova
A Warpwood Thunder Caller
 
Spawnova's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2012
Posts: 96
Perfect, thanks!
  Reply With Quote
06-18-13, 07:40 PM   #4
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,060
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.
  Reply With Quote
06-19-13, 02:29 AM   #5
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
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.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
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: 2,326
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
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
06-22-13, 02:12 AM   #7
humfras
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Oct 2009
Posts: 131
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
  Reply With Quote
06-22-13, 11:30 AM   #8
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
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

  Reply With Quote
06-23-13, 03:17 AM   #9
humfras
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Oct 2009
Posts: 131
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
  Reply With Quote
06-23-13, 11:46 AM   #10
Lombra
A Molten Giant
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 554
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!
  Reply With Quote
06-23-13, 04:57 PM   #11
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,060
Yes, anchoring really works. It's how I normally handle this.
  Reply With Quote

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


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