Aligning Text in Derma

So far I have tried many examples with DLabel[img_thumb]http://wiki.garrysmod.com/favicon.ico[/img_thumb], DTextEntry[img_thumb]http://wiki.garrysmod.com/favicon.ico[/img_thumb], P.HTML[img_thumb]http://wiki.garrysmod.com/favicon.ico[/img_thumb].

I am looking to place text in a menu and have it justified in the left, right, or center. So far I successfully used P.HTML to make a small box have the inventory names centered and have a color to represent the type of item it is. This is only good because it isn’t being displayed behind the custom texture that I want it’s info to be displayed over.

DLabel and DTextEntry work ok on what I want, but I can’t find anything to have text justified to the left, right, or center. P.HTML seems to have worked the best, but however I can’t disable the default white background it uses. I tried many things in http://www.w3schools.com but that annoying white background blocks the custom made textures I have made for the menu (example, setting a CSS background-color Property to transparent doesn’t work, and the white background is still displayed).

I don’t understand the question… Are you trying to draw text aligned to the left?
Or having control over where the text is aligned?

There are going to be parts of the menu where I have the text centered, aligned to the left and also aligned to the right. So basically having control over where the text is aligned is what I am looking for.

If you are drawing your own menu and/or menu items, you can use **[Draw.DrawText

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Draw.DrawText)**.

Should help you out.

Thanks for the info, but **[Panel.CenterHorizontal

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Panel.CenterHorizontal)** and **[Panel.CenterVertical

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Panel.CenterVertical)** doesn’t work in **[DTextEntry

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=DTextEntry)**. Do you have any other sugestions for centering text using Derma still??

Panel:Center()?
Or just do it manally.

Panel:Center() doesn’t center either using DTextEntry…

I will show you a picture of what I am trying to do and some code as well…

http://img440.imageshack.us/img440/1285/menucenterexample.jpg

[lua]

whq_item_name = vgui.Create( “DTextEntry”, whq_item_top )
whq_item_name:SetPos( 0, 0 )
whq_item_name:SetSize( 248, 48 )
whq_item_name:SetText( “Testing” )
whq_item_name:SetTextColor(Color(255, 0, 255, 255));
whq_item_name:SetWrap(true)
whq_item_name:CenterHorizontal()
whq_item_name:SetDrawBackground(false)
whq_item_name:SetDrawBorder(false)
whq_item_name:SetEditable(false)

[/lua]

I have tried a number of combinations that you said ‘Science’ but it doesn’t adjust the text to center properly. For that matter it doesn’t do anything. And adjusting it manually wouldn’t be good because there will be hundreds of these things to align, and the text size and character count will be different for each item. Maybe it is picky with using DTextEntry??

DLabel with MultiLine enabled? Would this solve your issues?

DLabel with MultiLine enabled does pretty much the same thing that DTextEntry with MulitLine enabled would do. I don’t know why but there are some subtle differences with enabling certain things and taking out parts of code too. I am still looking to find something that will automatically adjust the text and center it according by its character amount. Here are a few examples of the numerous things I have been experimenting with.

http://img16.imageshack.us/img16/2372/itemcentertest1.png

[lua]

whq_item_name = vgui.Create( “DTextEntry”, whq_item_top )
whq_item_name:SetPos( 0, 0 )
whq_item_name:SetSize( 248, 48 )
whq_item_name:SetText( “Testing” )
whq_item_name:SetTextColor(Color(255, 0, 255, 255));
whq_item_name:SetDrawBackground(false)
whq_item_name:SetDrawBorder(false)
whq_item_name:SetEditable(false)

whq_item_name:SetWrap(true)

[/lua]

http://img535.imageshack.us/img535/7135/itemcentertest2.png

[lua]

whq_item_name = vgui.Create( “DTextEntry”, whq_item_top )
whq_item_name:SetPos( 0, 0 )
whq_item_name:SetSize( 248, 48 )
whq_item_name:SetText( “Testing” )
whq_item_name:SetTextColor(Color(255, 0, 255, 255));
whq_item_name:SetDrawBackground(false)
whq_item_name:SetDrawBorder(false)
whq_item_name:SetEditable(false)

whq_item_name:SetMultiline(true)

[/lua]

http://img829.imageshack.us/img829/8052/itemcentertest3.png

[lua]

whq_item_name = vgui.Create( “DTextEntry”, whq_item_top )
whq_item_name:SetPos( 0, 0 )
whq_item_name:SetSize( 248, 48 )
whq_item_name:SetText( “Testing” )
whq_item_name:SetTextColor(Color(255, 0, 255, 255));
whq_item_name:SetDrawBackground(false)
whq_item_name:SetDrawBorder(false)
whq_item_name:SetEditable(false)

whq_item_name:CenterHorizontal()
whq_item_name:CenterVertical()

[/lua]

This is a little offtopic from your question, but I was wondering:

How do you intend to have the SetText filled with the item name? I have a similar need to have a derma-something with the item name on there, but I’m not certain how to call the name from the item table and have it auto-fill in there, as my attempts have only really gotten errors…
Just curious on how you’ll go about it too.

I read various articles on facepunch, gmod’s wiki, and some other lua websites and came up with this code. Also note that this is just an example, and probably not the best out there but it works fine for me right now.

[lua]

Game_Item = {}

for i = 1, 5 do
Game_Item* = {}
end

Game_Item[1].ItemName = ‘dagger’
Game_Item[2].ItemName = ‘sword’
Game_Item[3].ItemName = ‘mace’
Game_Item[4].ItemName = ‘sheild’
Game_Item[5].ItemName = ‘bow’

x = 3

whq_item_name = vgui.Create( “DTextEntry”, whq_item_top )
whq_item_name:SetPos( 0, 0 )
whq_item_name:SetSize( 248, 48 )
whq_item_name:SetText( “”…Game_Item.ItemName…"" )
whq_item_name:SetTextColor(Color(255, 0, 255, 255));
whq_item_name:SetDrawBackground(false)
whq_item_name:SetDrawBorder(false)
whq_item_name:SetEditable(false)
whq_item_name:SetWrap(true)

[/lua]

This should display text with the word ‘mace’ from the table. Make sure to put those addition quotation marks or apostrophes along with the double periods on the front and back.

[lua]

whq_item_name:SetText( “”…Game_Item.ItemName…"" )

[/lua]

I am still trying to find out how to center it properly though.

Finally I have been able to find something suitable to centering text in Derma.

I am now using DPanel[img_thumb]http://wiki.garrysmod.com/favicon.ico[/img_thumb] to do this and it works well. From there I am using DPanel.Paint[img_thumb]http://wiki.garrysmod.com/favicon.ico[/img_thumb] to call Draw.SimpleTextOutlined[img_thumb]http://wiki.garrysmod.com/favicon.ico[/img_thumb]. Then dividing the DPanel’s X,Y size by two, and having the Xalign and Yalign set to TEXT_ALIGN_CENTER, I was then able to get this.

http://img855.imageshack.us/img855/4935/itemcentertest4.png

[lua]

local whq_item_name = vgui.Create( “DPanel”, whq_item_top )
whq_item_name:SetPos( 0, 0 )
whq_item_name:SetSize( 248, 48 )
whq_item_name.Paint = function()
draw.SimpleTextOutlined( “”…Game_Item[1].ItemName…"", “Trebuchet20”, (248 / 2), (48 / 2), Color( 200, 0, 0, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 1, Color(0,0,0,255))
end

[/lua]

So I guess ‘vercas’ was somewhat correct on this. Thanks for your input too ‘Science’. If there is anything better then what I have did, using Derma, then I am still open to other ideas.