So say I have the string, "I want the word RED to be colored red." How could I make it so that the word RED is drawn red, but the rest is
drawn white? I know I would have to loop through the string somehow, but I cant quite figure out what I have to do.
Any help is appreciated!
Drawing different parts of a string with different colors.
-
- Prole
- Posts: 3
- Joined: Fri Nov 27, 2020 10:02 pm
Re: Drawing different parts of a string with different colors.
Code: Select all
str_table=
{
{text="I want the word ", color={1,1,1}},
{text="RED", color={1,0,0}},
{text=" to be colored red.", color={1,1,1}}
}
function printc (tabl, x,y)
font=love.graphics.getFont()
for i, v in ipairs (tabl) do
love.graphics.setColor(v.color)
love.graphics.print(v.text, x, y)
x=x+font:getWidth(v.text)
end
end
printc (str_table, 100, 300) -- not tested
Re: Drawing different parts of a string with different colors.
See love.graphics.printf#Synopsis_5.
Please note also that this forum is for posting your games and creations. Support questions should be posted in the Support and Development forum.
Please note also that this forum is for posting your games and creations. Support questions should be posted in the Support and Development forum.
-
- Prole
- Posts: 3
- Joined: Fri Nov 27, 2020 10:02 pm
Re: Drawing different parts of a string with different colors.
This is kind of what I want. I already have a table with a string, like this,darkfrei wrote: ↑Sat Mar 20, 2021 10:23 pmCode: Select all
str_table= { {text="I want the word ", color={1,1,1}}, {text="RED", color={1,0,0}}, {text=" to be colored red.", color={1,1,1}} } function printc (tabl, x,y) font=love.graphics.getFont() for i, v in ipairs (tabl) do love.graphics.setColor(v.color) love.graphics.print(v.text, x, y) x=x+font:getWidth(v.text) end end printc (str_table, 100, 300) -- not tested
text_table = {'I want to highlight the word RED'}
Is there any way I can loop through this string and set the word RED as red?
Re: Drawing different parts of a string with different colors.
You can use this split string function:
https://stackoverflow.com/questions/142 ... ing-in-lua
https://stackoverflow.com/questions/142 ... ing-in-lua
Code: Select all
function mysplit (inputstr, sep)
if sep == nil then
sep = "%s" -- space symbol
end
local t={}
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
if str== "RED" then print ('the RED str!') end
table.insert(t, str)
end
return t
end
Who is online
Users browsing this forum: No registered users and 0 guests