Text Wrap2 @ar2rsawseen

From GiderosMobile
Revision as of 20:02, 17 November 2023 by MoKaLux (talk | contribs) (Created page with "__TOC__ === Text Wrap2 @ar2rsawseen '''Class''' === <syntaxhighlight lang="lua"> -- ************************************************************* * This script is developed...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Text Wrap2 @ar2rsawseen Class

--[[
*************************************************************
* This script is developed by Arturs Sosins aka ar2rsawseen, http://appcodingeasy.com
* Feel free to distribute and modify code, but keep reference to its creator
*
* TextWrap object splits string into multiple lines by provided width.
* You can also specify line spacing and alignment modes : left, right, center and justify
* It is completely compatible with existing TextField object methods
*
* For more information, examples and online documentation visit: 
* http://appcodingeasy.com/Gideros-Mobile/Creating-multi-line-text-fields-in-Gideros-Mobile
**************************************************************

	function TextWrap:init(text, width, align, linespace, font)
	- text = string
	- width = number
	- align can be: "left", "right", "center", "justify"
	- linespace = number
	- font = Font
]]--

TextWrap = Core.class(Sprite)

function TextWrap:init(text, width, align, linespace, font)
	-- argument check
	if not align then align = "left" end
	if not linespace then linespace = 2 end
	if not font then font = nil end

	--internal settings
	self.textColor = 0x000000
	self.letterSpacing = 0.6
	self.text = text
	self.align = align
	self.width = width
	self.lineSpacing = linespace
	self.font = font
	
	self:setText(text)
end

function TextWrap:setText(text)
	self.text = text

	-- remove previous lines
	local childCount = self:getNumChildren()
	if childCount > 0 then
		for i = 0, childCount-1 do
			self:removeChildAt(childCount-i)
		end
	end

	-- some calculations
	local tf = TextField.new(self.font, self.text)
	tf:setLetterSpacing(self.letterSpacing)
	local textWidth = tf:getWidth()
	local letterCount = text:len()
	local letterChunks = math.floor(letterCount/(textWidth/self.width))
	local iters = math.ceil(letterCount/letterChunks)

	-- split string in lines
	local height = 0
	local last = 1
	for i = 1, iters do
		local part = text:sub(last, last+letterChunks-1)
		local lastSpace = 0
		local newLine = false
		local len = part:len()
		local startStr = part:find("\n")
		if startStr ~= nil then
			lastSpace = startStr - 1
			last = last + 1
			newLine = true
		end
		if lastSpace == 0 then
			-- finding last space
			for i = 1, len do
				if part:find(" ", -i) ~= nil then
					lastSpace = ((len-i)+1)
					break
				end
			end
		end
		if lastSpace > 0 and i ~= iters then
			last = last + lastSpace
			part = part:sub(1, lastSpace)
		else
			last = last + letterChunks
		end
		local line = TextField.new(self.font, part)
		line:setLetterSpacing(self.letterSpacing)
		line:setTextColor(self.textColor)
		if self.align == "left" or (newLine and self.align == "justify") then
			self:addChild(line)
			line:setPosition(0, height)
		elseif self.align == "right" then
			self:addChild(line)
			line:setPosition(self.width - line:getWidth(), height)
		elseif self.align == "center" then
			self:addChild(line)
			line:setPosition((self.width - line:getWidth())/2, height)
		elseif self.align == "justify" then
			local diff = self.width - line:getWidth()
			-- if no difference or last line
			if diff == 0 or i == iters then
				self:addChild(line)
				line:setPosition(0, height)
			else
				local res, spaceCount = part:gsub(" ", "")
				newLine = TextField.new(self.font, res)
				newLine:setLetterSpacing(self.letterSpacing)
				diff = self.width - newLine:getWidth()
				local eachSpace = diff/(spaceCount-1)
				local lastPos = 0
				for wordString in part:gmatch("[^%s]+") do
					local word = TextField.new(self.font, wordString)
					word:setLetterSpacing(self.letterSpacing)
					word:setTextColor(self.textColor)
					self:addChild(word)
					word:setPosition(lastPos, height)
					lastPos = lastPos + word:getWidth() + eachSpace
				end
			end
		end
		height = height + line:getHeight() + self.lineSpacing
	end
end

function TextWrap:getText()
	return self.text
end

function TextWrap:setTextColor(color)
	self.textColor = color
	for i = 1, self:getNumChildren() do
		local sprite = self:getChildAt(i)
		sprite:setTextColor(color)
	end
end

function TextWrap:getTextColor()
	return self.textColor
end

function TextWrap:setLetterSpacing(spacing)
	self.letterSpacing = spacing
	for i = 1, self:getNumChildren() do
		local sprite = self:getChildAt(i)
		sprite:setLetterSpacing(spacing)
	end
end

function TextWrap:getLetterSpacing()
	return self.letterSpacing
end

function TextWrap:setAlignment(align)
	self.align = align
	self:setText(self.text)
end

function TextWrap:getAlignment()
	return self.align
end

function TextWrap:setWidth(width)
	self.width = width
	self:setText(self.text)
end

function TextWrap:getWidth()
	return self.width
end

function TextWrap:setLineSpacing(linespace)
	self.lineSpacing = linespace
	self:setText(self.text)
end

function TextWrap:getLineSpacing()
	return self.lineSpacing
end

function TextWrap:setFont(font)
	self.font = font
	self:setText(self.text)
end

function TextWrap:getFont()
	return self.font
end

Text Wrap2 @ar2rsawseen Demo

application:setBackgroundColor(0x6c6c6c)

-- some vars
local width = application:getContentWidth()
local fonttitle = TTFont.new("fonts/tahoma.ttf", 32)
local font = TTFont.new("fonts/tahoma.ttf", 22)

-- main title
local maintitle = TextWrap.new("TextWrap", width, nil, 0.6, fonttitle)
maintitle:setLetterSpacing(8*3)
maintitle:setAlignment("center")
maintitle:setTextColor(0x66aaff)

-- left side
local ltext =
	"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc suscipit arcu placerat lorem iaculis bibendum."..
	"Phasellus eu urna non massa rutrum euismod. Proin ac scelerisque augue. Sed vitae augue mi. Cras mi tellus, auctor pulvinar"..
	"aliquet sed, suscipit eget justo. Nulla nec sem ac metus mollis ullamcorper eget eget dolor."..
	"In vitae metus quis ligula mollis blandit. Vestibulum est sem, ultricies id tempus et, sollicitudin eu augue."
local ltw = TextWrap.new(ltext, width/2-8, "left", 0.6, font)

-- right side
-- title
local rtwtitle = TextWrap.new("Title", width/2-8*2, "center", 0.6, font)
rtwtitle:setLetterSpacing(8*1)
rtwtitle:setTextColor(0xffaa66)
-- text
local rtw = TextWrap.new("Description: Lorem ipsum dolor sit amet, consectetur adipiscing elit.", width/2-8)
rtw:setAlignment("justify")
rtw:setLineSpacing(0.6)
rtw:setFont(font)
rtw:setTextColor(0xaaff66)

-- position
maintitle:setPosition(0, 32)
ltw:setPosition(0, 32*3) -- left side
rtwtitle:setPosition(width/2, 32*3) -- right side
rtw:setPosition(width/2, 32*4) -- right side
-- order
stage:addChild(maintitle)
stage:addChild(ltw) -- left side
stage:addChild(rtwtitle) -- right side
stage:addChild(rtw) -- right side


UI Text