In case this would be useful for anyone else, I added the ability to tint text to any desired color. This works especially well if your bitmap font texture file consists of white letters over a transparent background:
-- Pass a font object (obtained from loadFont) and a string to render -- Return value is a DisplayObject of the rendered string -- object.font can be read/modifed -- object.text can be read/modified -- object.align can be read/modified - left/right/center (multiline not yet fully supported for non-left) -- object.tint can be read/modified - SPACEWOLF -- object.input( function(text), { filter = function(), max = 32 } ) -- turns the object into a text input. -- the callback is hit when the user presses "return" or the field losed focus. -- this code is under development - more documentation will be added soon... function newString( font, text, newsize) --local finalScale = 100/origsize \*newsize/100; local finalScale = 100/font.info.size \*newsize/100; local obj = display.newGroup() accessorize( obj ) removerize( obj ) obj.set\_font = function( t, k, v ) obj.raw\_font = v if t.text then t.text = t.text end end -- Set the alignment of the object obj.set\_align = function( t, k, v ) local w = t.textWidth if t.raw\_align == 'right' then for i = 1, t.numChildren do t[i].x = t[i].x + w end elseif t.raw\_align == 'center' then for i = 1, t.numChildren do t[i].x = t[i].x + math.floor( w \* 0.5 ) end end t.raw\_align = v if t.raw\_align == 'right' then for i = 1, t.numChildren do t[i].x = t[i].x - w end elseif t.raw\_align == 'center' then for i = 1, t.numChildren do t[i].x = t[i].x - math.floor( w \* 0.5 ) end elseif t.raw\_align ~= 'left' then t.raw\_align = 'left' end end -- Set the text for the object obj.set\_text = function( t, k, v ) if ( v == t.raw\_text ) then return end t.raw\_text = v for i = t.numChildren, 1, -1 do t[i]:removeSelf() end local oldAlign = ( t.align or 'left' ) local oldTint = ( t.tint or {0, 0, 0, 0} ) t.align = 'left' local x = 0; local y = 0 local last = ''; local xMax = 0; local yMax = 0 if t.raw\_font then for c in string.gmatch( t.raw\_text..'\n', '(.)' ) do if c == '\n' then x = 0; y = y + t.raw\_font.info.lineHeight if y \>= yMax then yMax = y end elseif t.raw\_font.chars[c] then local rfc = t.raw\_font.chars[c] if 0 + t.raw\_font.chars[c].width \> 0 and 0 + t.raw\_font.chars[c].height \> 0 then local letter = sprite.newSprite( t.raw\_font.sprites[c] ) if t.raw\_font.kernings[last .. c] then x = x + font.kernings[last .. c] end t:insert( letter ) letter.anchorX = 0 letter.anchorY = 0 letter.x = t.raw\_font.chars[c].xoffset + x letter.y = t.raw\_font.chars[c].yoffset - t.raw\_font.info.base + y last = c end --x = x + t.raw\_font.chars[c].xadvance x = x + t.raw\_font.chars[c].xadvance + (t.raw\_font.info.outline or 0) --CLF added support for outlines if x \>= xMax then xMax = x end end end obj.textWidth = xMax end t.align = oldAlign t.tint = oldTint end -- set the tint of the object obj.set\_tint = function( t, k, v ) t.raw\_tint = v for i = t.numChildren, 1, -1 do if v[4] then t[i]:setFillColor(v[1],v[2],v[3],v[4]) else t[i]:setFillColor(v[1],v[2],v[3]) end end end obj.input = function( f, args ) -- spawn the text field invisibly local field -- Handle character insertion/deletion local function char() -- check if any character has been added or deleted if field.text ~= '--' then if string.len( field.text ) \< 2 then -- backspace was pressed if string.len( obj.text ) \> 0 then obj.text = string.sub( obj.text, 1, -2 ) end else -- some other key was pressed obj.text = obj.text..string.sub( field.text, 3 ) end field.text = '--' if args.filter then obj.text = string.sub( args.filter( obj.text ), 1, (args.max or 32) ) else obj.text = string.sub( obj.text, 1, (args.max or 32) ) end end end Runtime:addEventListener( 'enterFrame', char ) -- Handle the "done" phase local function done( e ) if e.phase == 'submitted' or e.phase == 'ended' then native.setKeyboardFocus( nil ) field:removeSelf() Runtime:removeEventListener( 'enterFrame', char ) f( text ) end end field = native.newTextField( 0, 0, 240, 24, done ) field.text = '--' field.isVisible = false native.setKeyboardFocus( field ) end obj.font = font obj.align = 'left' obj.text = (text or '') obj.xScale = finalScale; obj.yScale = finalScale; return obj end