Centering Text on the X axis only [SOLVED]

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Lolocks
Prole
Posts: 12
Joined: Tue Jul 15, 2014 3:06 pm

Centering Text on the X axis only [SOLVED]

Post 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
Last edited by Lolocks on Sat Jul 19, 2014 6:43 pm, edited 1 time in total.
User avatar
undef
Party member
Posts: 438
Joined: Mon Jun 10, 2013 3:09 pm
Location: Berlin
Contact:

Re: Centering Text on the X axis only

Post by undef »

There is love.graphics.printf for that.
twitter | steam | indieDB

Check out quadrant on Steam!
Lolocks
Prole
Posts: 12
Joined: Tue Jul 15, 2014 3:06 pm

Re: Centering Text on the X axis only

Post by Lolocks »

Last edited by Lolocks on Sat Jul 19, 2014 6:44 pm, edited 1 time in total.
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: Centering Text on the X axis only

Post 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
Last edited by MadByte on Sat Jul 19, 2014 6:33 pm, edited 4 times in total.
Zeliarden
Party member
Posts: 139
Joined: Tue Feb 28, 2012 4:40 pm

Re: Centering Text on the X axis only

Post by Zeliarden »

try

Code: Select all

love.graphics.print("Example Text", (love.graphics.getWidth( )/2-TitleFont:getWidth("Example Text")/2), 10)
Lolocks
Prole
Posts: 12
Joined: Tue Jul 15, 2014 3:06 pm

Re: Centering Text on the X axis only

Post 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!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 1 guest