Page 1 of 1

Centering Text on the X axis only [SOLVED]

Posted: Sat Jul 19, 2014 5:58 pm
by Lolocks
Sorry for all these threads

But anyways, I'm trying to center text on the X axis *only*. On one of my canvas' I tried getting the width of the font then dividing by 2. It looked pretty centered. So for another canvas, I used the same 'algorithm', however, it was no where near centered. It was completely visibly off.

How would I center something on the X axis only so I could change the Y axis as I please?

'Algorithm' being used:

Code: Select all

function love.load()
	TitleFont = love.graphics.newFont("/fonts/FaceYourFears.ttf", 45)
end

function love.draw()
	love.graphics.print("Example Text", (TitleFont:getWidth("Example Text")/2), 10)
end

Re: Centering Text on the X axis only

Posted: Sat Jul 19, 2014 6:09 pm
by undef
There is love.graphics.printf for that.

Re: Centering Text on the X axis only

Posted: Sat Jul 19, 2014 6:13 pm
by Lolocks

Re: Centering Text on the X axis only

Posted: Sat Jul 19, 2014 6:27 pm
by MadByte
You should try to read the wiki.
love.graphics.printf

my example to center text on screen:

Code: Select all

local font = love.graphics.newFont("font.ttf", 20)
local screenWidth, screenHeight = love.window.getDimensions()

function love.draw()
  love.graphics.printf("Hello World!", 0, screenHeight / 2 - font:getHeight() / 2, screenWidth, "center")
end

Re: Centering Text on the X axis only

Posted: Sat Jul 19, 2014 6:29 pm
by Zeliarden
try

Code: Select all

love.graphics.print("Example Text", (love.graphics.getWidth( )/2-TitleFont:getWidth("Example Text")/2), 10)

Re: Centering Text on the X axis only

Posted: Sat Jul 19, 2014 6:43 pm
by Lolocks
Zeliarden wrote:try

Code: Select all

love.graphics.print("Example Text", (love.graphics.getWidth( )/2-TitleFont:getWidth("Example Text")/2), 10)
Thanks for helping!