Posted: Fri Dec 21, 2018 4:29 pm Post subject: Auto-Center Label Text on Parent Panel
I want to center a text label on its parent panel & update its position to remain centered even if the parent is resized. I'm having trouble figuring out if this is possible.
Here is some example code:
Code:
local function showAbout()
-- dialog to display information about trainer
local aboutDialog = createForm(false)
aboutDialog.setCaption("About")
aboutDialog.setSize(400, 480)
-- main panel
local panel = createPanel(aboutDialog)
panel.Align = alClient
-- description text
local descr = createLabel(panel)
descr.setCaption("CE Trainer for a Game")
-- show the dialog
aboutDialog.showModal()
-- free memory after dialog is closed
aboutDialog.destroy()
end
I've tried setting descr.Align, descr.Alignment, & descr.Layout values (as well as those values for panel). But I haven't figured out how to get the text to center on the panel. Is this even possible. Or do I just have to set the text's position manually & disallow resizing the window?
Ah, thank you Dark Byte. I was just looking at the tutorial & figured out how to center the text in the form designer using anchors. But wasn't sure how to translate it over to Lua. I will try out your suggestions & see if I can find any more references to anchoring on the wiki.
-- Edit --
That worked:
Code:
local function showAbout()
-- dialog to display information about trainer
local aboutDialog = createForm(false)
aboutDialog.setCaption("About")
aboutDialog.setSize(400, 480)
-- main panel
local panel = createPanel(aboutDialog)
panel.Align = alClient
-- description text
local descr = createLabel(panel)
descr.setCaption("CE Trainer for a Game")
-- center text
descr.anchorSideLeft.control = panel
descr.anchorSideLeft.side = asrCenter
descr.anchorSideTop.control = panel
descr.anchorSideTop.side = asrCenter
-- show the dialog
aboutDialog.showModal()
-- free memory after dialog is closed
aboutDialog.destroy()
end
Thank you again Dark Byte.
-- Edit --
I'm having another small issue with this. In the form designer I can center justify the text by setting Alignment to taCenter. But adding descr.Alignment = taCenter to my code doesn't work.
-- Edit --
I should note that I am using a newline escape character in the text:
Code:
descr.setCaption("CE Trainer\nfor a Game")
-- Edit --
Doesn't seem to work with an actual line break either:
Code:
descr.setCaption("CE Trainer\
for a Game")
-- Solved --
Using lowercase (descr.alignment = taCenter) doesn't work either. However, I was able to get it to work via the setProperty function:
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum