View Single Post
Old 05-12-2016, 07:27 PM   #24
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,019
Default

Font size isn't measured in pixels; Arial @ 10 will be a different size than Calibri @ 10 will be a different size than Comic Sans @ 10, etc, etc. So if your code is working, I think it's by accident.

I had a look at my code above and it turns out I just forgot to re-set gfx.x and gfx.y for drawing the string. The following is working fine for me:

Code:
	local my_str = "this is kind of a long string"
	
	local font_sz = 30
	local pad = 8

	gfx.setfont(1, font, font_sz)

	gfx.rect(x,y,w,h,0)

	local str_w, str_h = gfx.measurestr(my_str)

	-- We don't want to run the text right to the edge of the button
	local max_length = w - (2 * pad)

	-- See if my_str at the current size will fit in the rectangle
	-- If it does, break the loop and carry on
	-- If it doesn't, reduce the font size by 1 and check again

	for i = font_sz, 0, -1 do
		
		if str_w <= max_length then break end

		gfx.setfont(1, font, i)
		str_w, str_h = gfx.measurestr(my_str)
	end
	
	gfx.x = x + (w - str_w) / 2
	gfx.y = y + (h - str_h) / 2
	gfx.drawstr(my_str)

Last edited by Lokasenna; 06-19-2017 at 03:41 PM.
Lokasenna is offline   Reply With Quote