My simple rich text code

I decided to do write a quick and dirty code for my rich text code needs despite the availability of some very interesting packages out there. It only covers colors, bold/regular, and font size options and is dependent on this package for color retrieval: https://github.com/andrewyavors/Lua-Color-Converter
Tweak it and improve it if you need to.

Untitled

-- textStyler.lua

local color = require("convertcolor") --A very nice package for accessing hex colors https://github.com/andrewyavors/Lua-Color-Converter
local M = {}

--Define display size
local centerX = display.contentCenterX
local centerY = display.contentCenterY
local _W = display.contentWidth
local _H = display.contentHeight


local myRect = display.newRoundedRect( centerX, centerY, _W*0.85, _H-2, 5 ) --Use as background and a reference point to position the final text
myRect:setFillColor(color.hex("696969"))
myRect.alpha = 0.5

local txtBox = display.newGroup()

local textStart = {}
textStart.x = 0
textStart.y = 100
textStart.width = myRect.contentWidth 
local textChain = {}
local currentLine = 0
local lineSpacing = 14
local wConter = 0

function M.textSplitter(input, inSplitPattern ) -- From Corona online manual
    local outResults = {}
    local theStart = 1
    local theSplitStart, theSplitEnd = string.find( input, inSplitPattern, theStart )
 
    while theSplitStart do
        table.insert( outResults, string.sub( input, theStart, theSplitStart-1 ) )
        theStart = theSplitEnd + 1
        theSplitStart, theSplitEnd = string.find( input, inSplitPattern, theStart )
    end
 
    table.insert( outResults, string.sub( input, theStart ) )
    return outResults
end

function M.textStyler(text, fontSize, textColor, style)
    local myTable = M.textSplitter(text, " ")
    --print (myTable)
 	for i = 1,#myTable do --read each word and add it to the screen
	    wCounter = table.maxn(textChain) + 1
	    --if (wCounter>1) then
    		myTable[i] = " "..myTable[i]
    	--end
    	
    	if (style=='b') then --activate bold font style
    		style = native.systemFontBold
    	end

    	local options = --options for creating a newText
    		{
	        text = myTable[i],
	        font = style,   
	        fontSize = fontSize,
	        align = "center" 
    		}
    
	    textChain[wCounter] = display.newText(options)
	    txtBox:insert(textChain[wCounter])
	    textChain[wCounter]:setFillColor(color.hex(textColor)) --use convert color to change text's color
	    textChain[wCounter].anchorX = 0 -- Choosing anchor to Zero for easier spacing calculations
	    if (wCounter>1) then
	    	if (textChain[wCounter-1].x + textChain[wCounter-1].width + textChain[wCounter].width > textStart.width ) then
	    		currentLine = currentLine + 1
				textChain[wCounter].x = 0
				textChain[wCounter].y = currentLine * lineSpacing 
			else
				textChain[wCounter].x = textChain[wCounter-1].x + textChain[wCounter-1].width 
				textChain[wCounter].y = currentLine * lineSpacing 
			end
	    else 
	    	textChain[wCounter].x = textStart.x 	
	    end		
	end
	myRect.height = txtBox.contentHeight + lineSpacing
	txtBox.y = myRect.y - myRect.contentHeight/2 + lineSpacing
	txtBox.x = myRect.x - myRect.contentWidth/2 
end


return M
---------------------------
--Example usage:
--Main.lua
local t = require("textstyler")

t.textStyler("Hello everyone! This is a simple Solar 2D code to mix words with different", 12, "ffffff")
t.textStyler("color,", 12, "ff0000", "b")
t.textStyler("style,", 12, "ffffff", "b")
t.textStyler("and size.", 18, "ffffff")

longText = "Hope you will find it useful in your coding. You can choose any color you want such as"
t.textStyler(longText, 12, "ffffff")
t.textStyler("green,", 12, "008000")
t.textStyler("blue,", 12, "0000FF")
t.textStyler("gold,", 12, "FFD700")
t.textStyler("etc.", 12, "ffffff")
6 Likes

Thank you for that great snippet. Bookmarking it for later use!

1 Like

You are very welcome.

Sianature

Thank you, very interesting.
Renato

1 Like