I would like to use the justify align for some texts, but here's what it produces:
![Image](https://dl.dropboxusercontent.com/u/6748697/Wanderer/love%202016-10-11%2017-18-06-65.png)
The last line is always justified too, making it quite weird. Is there any way to not justify this last line?
Thanks!
Code: Select all
function love.load()
font = love.graphics.newFont(16)
love.graphics.setFont(font)
myText = {
"This is just an",
"example text on how",
"justify could work.",
"You can add as many",
"lines as you want",
"and it should",
"still work, I think.",
"123456789"
}
--Just for easier viewing, gets the longest line as the width
local maxT = 0
for i, v in ipairs(myText) do
if font:getWidth(v) > maxT then
maxT = font:getWidth(v)
end
end
myRect = {
x = 20, y = 20, width = maxT
}
love.window.setMode(myRect.width+2*myRect.x, #myText*font:getHeight()+2*myRect.y)
end
function love.draw()
love.graphics.setColor(255, 0, 0, 105)
love.graphics.rectangle("line", myRect.x, myRect.y, myRect.width, font:getHeight()*#myText)
love.graphics.setColor(255, 255, 255, 255)
for i, v in ipairs(myText) do
local px = myRect.x
local py = myRect.y + (i-1)*font:getHeight()
local spaces = string.len(v)
local s = v:gsub(" ", "")
spaces = math.max(1, spaces - string.len(s)) --Amount of space characters in the line. Maxed to 1 to avoid dividing by 0
local dif = myRect.width - font:getWidth(s)
local off = 0
local t = v:split(" ")
for j, w in ipairs(t) do
love.graphics.print(w, math.floor(px + (j-1)*(dif/spaces) + off), py)
off = off + font:getWidth(w)
end
end
end
function string:split(c) --I actually wrote this one, it accounts for splitters of multiple sizes
local s = self
local t = {""}
local skip = 0
for i = 1, string.len(s)+1-string.len(c) do
local v = string.sub(s, i, i-1+string.len(c))
if skip > 0 then
skip = skip-1
elseif v == c then
table.insert(t, "")
skip = string.len(c)-1
else
local lm = i
if i == string.len(s)+1-string.len(c) then
lm = -1
end
t[#t] = t[#t] .. string.sub(s, i, lm)
end
end
return t
end
Yep I also spotted this issue, but it's a lot less problematic than the other. And thanks for you tips, but I'll probably stick to a left align for now, I don't want to go to something too tricky for now.davisdude wrote:It also looks like it's not justifying right. If you look closely, it seems like the space in the second line is being included in the text width...
Users browsing this forum: Bing [Bot] and 3 guests