SetContentAlignment Help

Please see added message under the image. Why does SetWrap(true) stop it aligning properly?

I am trying to use SetContentAlignment on a DLabel but unsure why it is only setting the text on the left :confused:

This is my code:
announcementMessageLabel has announcementMessageLabel:SetContentAlignment( 5 ), surely that should mean the text is centred in the box?



function drawAnnouncement( playerNick, announcementMessageContent, backgroundEnabled )
    local announcementBackgroundPanel = vgui.Create( "DPanel" )
    announcementBackgroundPanel:SetSize( ScrW(), 200 )
    announcementBackgroundPanel:SetPos( 0, -200 )WA
    local announcementBackgroundX, announcementBackgroundY = announcementBackgroundPanel:GetSize()
    announcementBackgroundPanel.Paint = function()
        if backgroundEnabled == 1 then
            surface.SetDrawColor( 0, 0, 0, 125 ) 
        else
            surface.SetDrawColor( 0, 0, 0, 0 )
        end
        surface.DrawRect( 0, 0, announcementBackgroundPanel:GetWide(), announcementBackgroundPanel:GetTall() )
    end
    announcementBackgroundPanel:MoveTo( 0, 0, 0.1, 5, 1 )

    local announcementNameLabel = vgui.Create( "DLabel" )
    announcementNameLabel:SetFont( "playerNickFont" )
    announcementNameLabel:SetText( playerNick )
    announcementNameLabel:SizeToContents()
    announcementNameLabel:SetContentAlignment( 5 )
    local announcementNameX, announcementNameY = announcementNameLabel:GetSize()
    announcementNameLabel:SetPos( (ScrW()/2)-(announcementNameX/2), (0-50) )
    announcementNameLabel:SetTextColor( Color( 255, 255, 255, 255 ) )
    announcementNameLabel:MoveTo( (ScrW()/2)-(announcementNameX/2), 10, 0.1, 5.3, 1 )

    local announcementMessageLabel = vgui.Create( "DLabel" )
    announcementMessageLabel.Paint = function()
        surface.SetDrawColor( 255, 0, 0, 255 ) 
        surface.DrawRect( 0, 0, announcementMessageLabel:GetWide(), announcementMessageLabel:GetTall() )
    end
    announcementMessageLabel:SetSize( (ScrW()*0.65), announcementBackgroundY-(announcementNameY+40) )
    announcementMessageLabel:SetFont( "announcementMessageFont" )
    announcementMessageLabel:SetText( announcementMessageContent )
    announcementMessageLabel:SetWrap(true)
    announcementMessageLabel:SetContentAlignment( 5 )
    announcementMessageLabel:SetTextColor( Color( 255, 255, 255, 255 ) )
    local announcementMessageX, announcementMessageY = announcementMessageLabel:GetSize()
    announcementMessageLabel:SetPos( (ScrW()/2)-(announcementMessageX/2), (0-announcementMessageY) )
    announcementMessageLabel:MoveTo( (ScrW()/2)-(announcementMessageX/2), (20+announcementNameY), 0.1, 5.3, 1 )
    
    timer.Create( "removeAnnouncement", 10, 1, function()
        announcementNameLabel:MoveTo( (ScrW()/2)-(announcementNameX/2), (0-50), 0.1, 5, 1 )
        announcementMessageLabel:MoveTo( (ScrW()/2)-(announcementMessageX/2), (0-announcementMessageY), 0.1, 5, 1 )
        announcementBackgroundPanel:MoveTo( 0, -200, 0.1, 5.3, 1 )
        timer.Create( "deleteAnnouncement", 15, 1, function()
            announcementBackgroundPanel:Remove()
            announcementNameLabel:Remove()
            announcementMessageLabel:Remove()
        end)
    end)
end


And this is what I get :confused:

Any help would be appreciated :slight_smile:

[editline]25th May 2014[/editline]

Okay so been doing some testing, it appears as soon as I put announcementMessageLabel:SetWrap(true) in my announcementMessageLabel it doesn’t align it to the center? Anyone know why?

I don’t know why, just that it’s always been like that. My workaround has always been to instead of DLabel use DPanel and override its Paint function to paint your own text.

Strange, I’ll have to give that a go. Thanks for the reply.

Set your content alignment to 7 to get it top-left. The alignment is based off the numbers on your numpad.

I know :slight_smile: I want it aligned centre ‘announcementMessageLabel:SetContentAlignment( 5 )’ however it’s aligning to the left centre.

I have made my own Label panel to workaround this. Maybe it isn’t reflecting all the functionality of DLabel's, but text alignment and wrapping works well.

CODE

local LABEL = {}
local cw = Color(255, 255, 255)

function LABEL:Paint(w,h)
    if not self.text then return end

    local posx, posy
    if self.ay == TEXT_ALIGN_TOP then
        posy = 0
    elseif self.ay == TEXT_ALIGN_BOTTOM then
        posy = h - self.text_h
    else
        posy = h/2 - self.text_h / 2
    end

    if self.ax == TEXT_ALIGN_LEFT then
        posx = 0
    elseif self.ax == TEXT_ALIGN_RIGHT then
        posx = w
    else
        posx = w/2
    end
    
    local y = posy
    for _, line in ipairs(self.text) do
        draw.DrawText(line, self.font or "Arial", posx, y, self.color or cw, self.ax or TEXT_ALIGN_CENTER)
        y = y + self.line_h + (self.space or 1)
    end
end

function LABEL:PerformLayout(w, h)
    if not isstring(self.text_str) then return end
    self:SetText(self.text_str)
end

function LABEL:SetText(text)
    surface.SetFont(self.font or "Arial")
    local str = ""
    local tall = 0
    local first = true
    for _, w in ipairs(string.Split(text, " ")) do
        local first = #str == 0
        local split = string.Split(str, "\n")
        local line = (split[#split]) .. (first and "" or " ") .. w
        local wide, ntall = surface.GetTextSize(line)
        local bool = wide > self:GetWide()

        if tall < ntall then tall = ntall end
        str = str .. (bool and "\n" or (first and "" or " ")) .. w
    end
    
    local text_tbl = string.Split(str, "\n")
    self.text_h = #text_tbl * tall + (#text_tbl - 1) * (self.space or 1)
    self.line_h = tall
    self.text = text_tbl
    self.text_str = text
end

function LABEL:SetFont(font)
    self.font = font
    self:InvalidateLayout()
end

function LABEL:SetLineSpace(space)
    self.space = space
    self:InvalidateLayout()
end

function LABEL:SetTextColor(color)
    self.color = color
end

function LABEL:SetAlignment(a)
    if a == 1 then
        self.ax = TEXT_ALIGN_LEFT
        self.ay = TEXT_ALIGN_BOTTOM

    elseif a == 2 then
        self.ax = TEXT_ALIGN_CENTER
        self.ay = TEXT_ALIGN_BOTTOM

    elseif a == 3 then
        self.ax = TEXT_ALIGN_RIGHT
        self.ay = TEXT_ALIGN_BOTTOM

    elseif a == 4 then
        self.ax = TEXT_ALIGN_LEFT
        self.ay = TEXT_ALIGN_CENTER

    elseif a == 5 then
        self.ax = TEXT_ALIGN_CENTER
        self.ay = TEXT_ALIGN_CENTER

    elseif a == 6 then
        self.ax = TEXT_ALIGN_RIGHT
        self.ay = TEXT_ALIGN_CENTER

    elseif a == 7 then
        self.ax = TEXT_ALIGN_LEFT
        self.ay = TEXT_ALIGN_TOP

    elseif a == 8 then
        self.ax = TEXT_ALIGN_CENTER
        self.ay = TEXT_ALIGN_TOP

    elseif a == 9 then
        self.ax = TEXT_ALIGN_RIGHT
        self.ay = TEXT_ALIGN_TOP
    end
end

vgui.Register("DLabelWrap", LABEL, "DPanel")

USAGE

concommand.Add("label_test", function()
    local frame = vgui.Create("DFrame")
    frame:SetSize(300, 100)
    frame:MakePopup()
    frame:Center()

    local label = frame:Add("DLabelWrap")
    label:SetFont("uts.ui.pda.Label")
    label:SetAlignment(5)
    label:SetLineSpace(-5)
    label:SetPos(0, 0)
    label:SetSize(150, 100)
    label:SetText("Wrapped text with space -5")

    label = frame:Add("DLabelWrap")
    label:SetFont("uts.ui.pda.Label")
    label:SetAlignment(5)
    label:SetPos(150, 0)
    label:SetSize(150, 100)
    label:SetText("Wrapped text with default space")
end)

image