• TEXT_ALIGN_CENTER with derma:SetText()
    12 replies, posted
I want to use derma:SetText("text") for example, but I can't center it (horizontally at least) because well, there's no option for that with SetText. It needs to be derma for both ease of use and because I already have spent a long time coding this derma thing... so yes, how do I do it? I am using DLabel.
[url]http://wiki.garrysmod.com/page/Panel/CenterHorizontal[/url] ?
I doubt it's possible to do with the default derma control. In [url=http://facepunch.com/showthread.php?t=1151299]this[/url] thread the OP decided to go with overriding the Paint function of the panel (see the last post)
DLabel:SetContentAlignment( 5 ) should do the trick.
Robotboy is right and it won't do the trick, why not do some math? Getting lenght of text isn't hard, just sub half of it from parent panels width and it'll be centered.
Here's the problem. All of these things do the exact same thing: What happens is it just always [i]starts[/i] the text a little bit to the left of the center. It does it regardless of size and that is the problem. [editline]edit[/editline] It has no parent panels.
Override the Panel:Paint() hook, and use [URL="http://wiki.garrysmod.com/page/draw/SimpleText"]draw.SimpleText()[/URL] example: [LUA] function PANEL:Paint() draw.SimpleText( self:GetText(), "Arial12", self:GetWide()/2, self:GetTall()/2, Color(255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER ) end [/LUA]
[QUOTE=syl0r;46058742]Override the Panel:Paint() hook, and use [URL="http://wiki.garrysmod.com/page/draw/SimpleText"]draw.SimpleText()[/URL] example: [LUA] function PANEL:Paint(w, h) draw.SimpleText( self:GetText(), "Arial12", w/2, h/2, Color(255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER ) end [/LUA][/QUOTE] Adjusted your self:GetWide() and self:GetTall() calls into w and h, since they're passed as arguments to the Paint function.
[lua]local blah = vgui.Create("DLabel", myParent) blah:SetPos(0,0) // Position blah:SetColor(Color(255,255,255,255)) // Color blah:SetContentAlignment( 5 ) blah:SetFont("coolvetica") //blah:SetWrap(true) function blah:Paint() draw.SimpleText( self:GetText(), "coolvetica", self:GetWide()/2, self:GetTall()/2, Color(255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER ) end blah:SizeToContents() // make the control the same size as the text.[/lua] text is in top left...
[QUOTE=RonanZer0;46059006][lua]local blah = vgui.Create("DLabel", myParent) blah:SetPos(0,0) // Position blah:SetColor(Color(255,255,255,255)) // Color blah:SetContentAlignment( 5 ) blah:SetFont("coolvetica") //blah:SetWrap(true) function blah:Paint() draw.SimpleText( self:GetText(), "coolvetica", self:GetWide()/2, self:GetTall()/2, Color(255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER ) end blah:SizeToContents() // make the control the same size as the text.[/lua] text is in top left...[/QUOTE] Yes, because you set the labels position to 0,0 (top left) and tell it to size itself to its contents. Use blah:SetSize(blah:GetParent():GetWide(), blahParent:GetParent():GetTall())
[QUOTE=Khub;46059071]Yes, because you set the labels position to 0,0 (top left) and tell it to size itself to its contents. Use blah:SetSize(blah:GetParent():GetWide(), blahParent:GetParent():GetTall())[/QUOTE] Seriously. I don't have a parent. I already stated it in the 6th post... I want it to center the screen, not the parent.
[QUOTE=RonanZer0;46059157]Seriously. I don't have a parent. I already stated it in the 6th post... I want it to center the screen, not the parent.[/QUOTE] Then set it to the size of the screen, not to the size of the text. [lua]blah:SetSize(ScrW(),ScrH())[/lua] and don't do SizeToContents()
[QUOTE=RonanZer0;46059157]Seriously. I don't have a parent. I already stated it in the 6th post... I want it to center the screen, not the parent.[/QUOTE] There's a chance my code would still work since all elements created without a panel are adopted by the vgui.GetWorldPanel(). Not really sure if :GetParent() returns that one or nil, though.
Sorry, you need to Log In to post a reply to this thread.