SmartText - ModuleScript for Automatic RichText Formatting

What is SmartText?

SmartText is an open-sourced ModuleScript that makes formatting text using RichText simple. In fact, SmartText will do all the formatting for you!

Why use SmartText?

SmartText handles all the text formatting for you, so that you can focus on what’s important: the text. All you have to do is insert the ModuleScript, require it, and use it’s functions.

How to get started?

Get started with SmartText in three simple steps:

ExplorerWindow

  • Require it from another script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SmartText = require(ReplicatedStorage:WaitForChild("SmartText")

How to use SmartText?

SmartText can automatically format your text with functions, and all you have to do is pass in the text you want to be formatted. Then, SmartText will return the properly formatted version of the text you passed in. Some of SmartText’s function will require more than one argument, such as it’s :Size() function. Bellow, you’ll find a list of all SmartText’s functions. A GitHub for SmartText’s documentation is coming soon.

SmartText:Bold("Text") -- returns in bold format
SmartText:Italic("Text") -- returns in an italic format
SmartText:Underline("Text") -- returns in an underlined format
SmartText:Strike("Text") -- returns in a strike-through format
SmartText:Size("Text", 10) -- returns text with size passed through
SmartText:ColorRGB("Text", 0, 0, 0) -- returns text in an RGB colored format
SmartText:ColorHEX("Text", "#123456") -- returns text in a HEX corored format

Example #1

LocalScript inside of a TextLabel:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SmartText = require(ReplicatedStorage:WaitForChild("SmartText"))

local bold = SmartText:Bold(":Bold ")
local italic = SmartText:Italic(":Italic ")
local line = SmartText:Underline(":Underline ")
local strike = SmartText:Strike(":Strike ")
local size = SmartText:Size(":Size ", 50)
local rgb = SmartText:ColorRGB(":ColorRGB ", 200, 100, 100)
local hex = SmartText:ColorHEX(":ColorHEX ", "#138dff")
local face = SmartText:Face(":Face ", "Bangers")
    
script.Parent.Text = bold .. italic .. line .. strike .. size .. rgb .. hex .. face

Result:

Place file for this example: SmartTextDemo.rbxl (24.0 KB)

Example #2

LocalScript inside of a TextLabel:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SmartText = require(ReplicatedStorage:WaitForChild("SmartText"))

local text1 = "SmartText"
text1 = SmartText:Bold(text1)
text1 = SmartText:Size(text1, 30)
text1 = SmartText:Face(text1, "Kalam")
text1 = SmartText:ColorRGB(text1, 100, 200, 200)

local text2 = "automatic formatting"
text2 = SmartText:Bold(text2)
text2 = SmartText:ColorRGB(text2, 100, 200, 200)

script.Parent.Text = "By using " .. text1 .. ", you can easily create amazing looking text with " .. text2

Result:

Place file for this example:SmartTextDemo.rbxl (24.0 KB)

24 Likes

This is cool. Have you considered using a metatable to allow for chaining? You could do something like this with chaining:

SmartText("Hello world"):Bold():Italic():Size(50):Face("Gotham")
3 Likes

That’s a really good idea! I will definitely look into that :smiley:

1 Like

Nice work! Recommend something like this when you implement chaining, there are some cool metamethods you could use to make this even more functional:

e.g. module:

local module = { }

function module.new(txt)
	local this = {class = 'module', text = txt or ''}
	setmetatable(this, {
		__index = this;
		__tostring = function ()
			return this.text
		end;
		__concat = function (a, b)
			if type(a) == 'string' or type(b) == 'string' then
				return a == this and this.text .. b or a .. this.text
			elseif type(a) == 'table' and type(b) == 'table' then
				if a.class and b.class and a.class == 'module' and b.class == 'module' then
					return module.new(a.text .. b.text)
				end
			end
			return warn 'Attmept to concat class invalid'
		end;
	})

	function this:bold(str)
		if str then
			this.text = this.text .. '<b>' .. str .. '</b>'
		else
			this.text = '<b>' .. str .. '</b>'
		end

		return this
	end

	function this:size(str, sz)
		if sz then
			this.text = this.text .. string.format('<font size="%d">', sz or 14) .. (str or '') .. '</font>'
		elseif not sz then
			this.text = string.format('<font size="%d">', type(str) == 'number' and str or 14) .. this.text .. '</font>'
		end

		return this
	end

	function this:concat(str)
		this.text = this.text .. str
		return this
	end

	return this
end

return module

e.g. some script

local module = require(--[[the_module]])

local str = module.new()
str:bold('Hello,'):concat(' '):size('world!', 50)

print(str) --> <b>Hello,</b> <font size="50">world!</font>

local cvc = module.new():bold('Lua woo --> ') .. str
print(cvc) --> <b>Lua woo --> </b><b>Hello,</b> <font size="50">world!</font>

local svc = 'Lua woo --> ' .. str
print(svc) -->  Lua woo --> <b>Hello,</b> <font size="50">world!</font>
2 Likes

Newer version of SmartText that allows for chaining! Thanks for the idea guys :smiley: