Globally set text color

Is there a method to globally set the text color, or a different method so I would not have to declare it after each display.newText? I was looking in display.setDefault() but only saw it for backgrounds and shapes.

You could do something like this:

local centerX = display.contentCenterX local centerY = display.contentCenterY local textGroup = display.newGroup() local textTable = {} local function spawnText(params) local object = display.newText(params.text, params.x, params.y, params.font, params.fontSize) --object:addEventListener("touch",touchListener) --Set the objects table to a table passed in by parameters object.objTable = params.objTable --Automatically set the table index to be inserted into the next available table index object.index = #object.objTable + 1 object.x = params.x or display.contentCenterX object.y = params.y or display.contentCenterY object.width = params.width or 100 object.height = params.height or 100 object.r = params.r or 1 object.g = params.g or 1 object.b = params.b or 1 object.fontSize = params.fontSize or 40 object.font = params.font or nil object.text = params.text or "No text specified." object:setFillColor(params.r,params.g,params.b) object.group = params.group or nil object.group:insert(object) object.objTable[object.index] = object return object end local function textSpawn(myText, newX, newY, rColor, gColor, bColor) local spawns = spawnText( { text = myText, objTable = textTable, group = textGroup, x = newX, y = newY, r = rColor, g = gColor, b = bColor, width = display.contentWidth, height = 100 } ) end textSpawn("Here is some text.", centerX, centerY, 1,0,0)

You can change it to your liking. Once you have the spawnText() and textSpawn() functions how you want them, anytime you need new text on the screen you can add the last line from above and put everything in one line of code. Those functions also add your text objects to a group and a table. Just a thought.

display.setDefault(“fillColor”, r, g, b, alpha) works also on text since text is a graphics object in Corona.

Will that not apply it to all other objects you create as well though?

Yes, that’s true (and the reason why I was bitching about it to CoronaLabs during the Graphics 2.0 beta phase). Text objects should be treated as their own entity. However that fell of deaf ears…

But it’s a way to globally set the text object color as the OP was requesting (with the drawback that it affects all other primitives as well).

Still thats pretty interesting. Thanks for your replies.

This isn’t the world’s most elegant solution, but it does work. If you add the below code to your main.lua, all your newText objects would be red by default:

display.newText0 = display.newText function display.newText(...) local textObject = display.newText0(...) textObject:setFillColor(1, 0, 0, 1) return textObject end

You could change the RGBA values in line 4 in the above snippet to whatever you want, and from that moment on, any new text objects (and only text objects) would be set to that color by default.

Hope this helps you out!

Shucks… I have a similar solution in place :)  but I use a global in my config.lua for each project.

config.lua

DEFAULT_TEXT_COLOR = {1,1,1,1}   – default white, alpha 1

utilities.lua

display.newText\_original = display.newText function display.newText(...) local obj = display.newText\_original(...) obj:setFillColor(unpack(DEFAULT\_TEXT\_COLOR)) return obj end

I require utilities in my other modules, and if ever I want to change the default text color I just set DEFAULT_TEXT_COLOR to what I want before calling newText().

You could do something like this:

local centerX = display.contentCenterX local centerY = display.contentCenterY local textGroup = display.newGroup() local textTable = {} local function spawnText(params) local object = display.newText(params.text, params.x, params.y, params.font, params.fontSize) --object:addEventListener("touch",touchListener) --Set the objects table to a table passed in by parameters object.objTable = params.objTable --Automatically set the table index to be inserted into the next available table index object.index = #object.objTable + 1 object.x = params.x or display.contentCenterX object.y = params.y or display.contentCenterY object.width = params.width or 100 object.height = params.height or 100 object.r = params.r or 1 object.g = params.g or 1 object.b = params.b or 1 object.fontSize = params.fontSize or 40 object.font = params.font or nil object.text = params.text or "No text specified." object:setFillColor(params.r,params.g,params.b) object.group = params.group or nil object.group:insert(object) object.objTable[object.index] = object return object end local function textSpawn(myText, newX, newY, rColor, gColor, bColor) local spawns = spawnText( { text = myText, objTable = textTable, group = textGroup, x = newX, y = newY, r = rColor, g = gColor, b = bColor, width = display.contentWidth, height = 100 } ) end textSpawn("Here is some text.", centerX, centerY, 1,0,0)

You can change it to your liking. Once you have the spawnText() and textSpawn() functions how you want them, anytime you need new text on the screen you can add the last line from above and put everything in one line of code. Those functions also add your text objects to a group and a table. Just a thought.

display.setDefault(“fillColor”, r, g, b, alpha) works also on text since text is a graphics object in Corona.

Will that not apply it to all other objects you create as well though?

Yes, that’s true (and the reason why I was bitching about it to CoronaLabs during the Graphics 2.0 beta phase). Text objects should be treated as their own entity. However that fell of deaf ears…

But it’s a way to globally set the text object color as the OP was requesting (with the drawback that it affects all other primitives as well).

Still thats pretty interesting. Thanks for your replies.

This isn’t the world’s most elegant solution, but it does work. If you add the below code to your main.lua, all your newText objects would be red by default:

display.newText0 = display.newText function display.newText(...) local textObject = display.newText0(...) textObject:setFillColor(1, 0, 0, 1) return textObject end

You could change the RGBA values in line 4 in the above snippet to whatever you want, and from that moment on, any new text objects (and only text objects) would be set to that color by default.

Hope this helps you out!

Shucks… I have a similar solution in place :)  but I use a global in my config.lua for each project.

config.lua

DEFAULT_TEXT_COLOR = {1,1,1,1}   – default white, alpha 1

utilities.lua

display.newText\_original = display.newText function display.newText(...) local obj = display.newText\_original(...) obj:setFillColor(unpack(DEFAULT\_TEXT\_COLOR)) return obj end

I require utilities in my other modules, and if ever I want to change the default text color I just set DEFAULT_TEXT_COLOR to what I want before calling newText().