• DLabel center align text
    9 replies, posted
Heya. Simple question: How do I align DLabel's text? [IMG]http://i.imgur.com/0OtRecw.png[/IMG] This is my code: [CODE] testLabel = vgui.Create("DLabel") testLabel:SetPos(0,25) testLabel:SetText("This is line 1\nAnd this is line 2") testLabel:SetContentAlignment(5) testLabel:SizeToContents() testLabel:CenterHorizontal(0.5)[/CODE] Thanks in advance~
[lua]testLabel:Dock(FILL)[/lua]
[QUOTE=sannys;51357946][lua]testLabel:Dock(FILL)[/lua][/QUOTE] Still the same, but now a bit lower than before. If I wasn't specific enough: I don't want to center the label in the panel but the text [I]within[/I] the label like so: [IMG]https://i.gyazo.com/193d104c374987cdc5681622c2ef0a4d.png[/IMG]
[QUOTE=Remixful;51357993]Still the same, but now a bit lower than before. If I wasn't specific enough: I don't want to center the label in the panel but the text [I]within[/I] the label like so: [IMG]https://i.gyazo.com/193d104c374987cdc5681622c2ef0a4d.png[/IMG][/QUOTE] If that's not working for you, you're doing something wrong. The label can't just draw its contents in the center of the panel if the label itself is not in the center. If you want the text to be dead center, the label itself has to not only be centered horizontally with the window, but also [I]vertically[/I] with the window, which is what docking does in this case. You could also use testLabel:Center. It really just depends on what you're doing in your actual code and not this example code. Docking works fine for me in this example. [t]http://i.imgur.com/FcMzWTi.png[/t]
[QUOTE=sannys;51358036]If that's not working for you, you're doing something wrong. [/QUOTE] You do understand I'm trying to work with multiple lines of text right? Not with just one line. Sorry I didn't include that, I was implying that's what I want in the picture I provided. [IMG]http://i.imgur.com/207nwIv.png[/IMG]
I've face this problem...Too...Many...Times DLabel has issues with doing the job, i recommend you to use this [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/markup/Parse]markup.Parse[/url]
It's tricky to do with a DLabel, but it works with [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/draw/DrawText]draw.DrawText[/url] if you set the xAlign
[QUOTE=gonzalolog;51358884]I've face this problem...Too...Many...Times DLabel has issues with doing the job, i recommend you to use this [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/markup/Parse]markup.Parse[/url][/QUOTE] I'm not sure how you wanted me to go about this but I tried [code] function mainframe:PaintOver(w,h) local parsed = markup.Parse( "<center>This text\n will be center-aligned.</center>" ) parsed:Draw(w/2,h/2,TEXT_ALIGN_CENTER,TEXT_ALIGN_CENTER, 255) end [/code] And this is the result I got: [IMG]http://i.imgur.com/kJccHC7.png[/IMG] I'm probably doing it wrong - but at this rate I might as well use draw.DrawText if I'm already painting over the panel. [code] local myText = "This text\nwill be center-aligned" surface.SetFont("DermaDefault") local height = select(2, surface.GetTextSize(myText)) draw.DrawText(myText, "DermaDefault", w/2,h/2 - height / 2, Color(255,125,125), TEXT_ALIGN_CENTER) [/code] Probably not the best solution but it works, right? [IMG]http://i.imgur.com/ZK7In9s.png[/IMG] This problem is solved, but I'll leave this thread open in case someone has a better solution.
If you're not planning to use the DLabel for anything such as do something when clicked on then DrawText might be the best option - and if you do want it clickable then you could just use DrawText on a panel sized to fit the text
Sorry, you need to Log In to post a reply to this thread.