<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://wiki.gideros.rocks/index.php?action=history&amp;feed=atom&amp;title=Text_Wrap2_%40ar2rsawseen</id>
	<title>Text Wrap2 @ar2rsawseen - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.gideros.rocks/index.php?action=history&amp;feed=atom&amp;title=Text_Wrap2_%40ar2rsawseen"/>
	<link rel="alternate" type="text/html" href="https://wiki.gideros.rocks/index.php?title=Text_Wrap2_@ar2rsawseen&amp;action=history"/>
	<updated>2026-11-18T02:23:06Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.11</generator>
	<entry>
		<id>https://wiki.gideros.rocks/index.php?title=Text_Wrap2_@ar2rsawseen&amp;diff=23030&amp;oldid=prev</id>
		<title>MoKaLux: Created page with &quot;__TOC__ === Text Wrap2 @ar2rsawseen &#039;&#039;&#039;Class&#039;&#039;&#039; === &lt;syntaxhighlight lang=&quot;lua&quot;&gt; -- ************************************************************* * This script is developed...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.gideros.rocks/index.php?title=Text_Wrap2_@ar2rsawseen&amp;diff=23030&amp;oldid=prev"/>
		<updated>2023-11-17T18:02:37Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;__TOC__ === Text Wrap2 @ar2rsawseen &amp;#039;&amp;#039;&amp;#039;Class&amp;#039;&amp;#039;&amp;#039; === &amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt; -- ************************************************************* * This script is developed...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;__TOC__&lt;br /&gt;
=== Text Wrap2 @ar2rsawseen &amp;#039;&amp;#039;&amp;#039;Class&amp;#039;&amp;#039;&amp;#039; ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
--[[&lt;br /&gt;
*************************************************************&lt;br /&gt;
* This script is developed by Arturs Sosins aka ar2rsawseen, http://appcodingeasy.com&lt;br /&gt;
* Feel free to distribute and modify code, but keep reference to its creator&lt;br /&gt;
*&lt;br /&gt;
* TextWrap object splits string into multiple lines by provided width.&lt;br /&gt;
* You can also specify line spacing and alignment modes : left, right, center and justify&lt;br /&gt;
* It is completely compatible with existing TextField object methods&lt;br /&gt;
*&lt;br /&gt;
* For more information, examples and online documentation visit: &lt;br /&gt;
* http://appcodingeasy.com/Gideros-Mobile/Creating-multi-line-text-fields-in-Gideros-Mobile&lt;br /&gt;
**************************************************************&lt;br /&gt;
&lt;br /&gt;
	function TextWrap:init(text, width, align, linespace, font)&lt;br /&gt;
	- text = string&lt;br /&gt;
	- width = number&lt;br /&gt;
	- align can be: &amp;quot;left&amp;quot;, &amp;quot;right&amp;quot;, &amp;quot;center&amp;quot;, &amp;quot;justify&amp;quot;&lt;br /&gt;
	- linespace = number&lt;br /&gt;
	- font = Font&lt;br /&gt;
]]--&lt;br /&gt;
&lt;br /&gt;
TextWrap = Core.class(Sprite)&lt;br /&gt;
&lt;br /&gt;
function TextWrap:init(text, width, align, linespace, font)&lt;br /&gt;
	-- argument check&lt;br /&gt;
	if not align then align = &amp;quot;left&amp;quot; end&lt;br /&gt;
	if not linespace then linespace = 2 end&lt;br /&gt;
	if not font then font = nil end&lt;br /&gt;
&lt;br /&gt;
	--internal settings&lt;br /&gt;
	self.textColor = 0x000000&lt;br /&gt;
	self.letterSpacing = 0.6&lt;br /&gt;
	self.text = text&lt;br /&gt;
	self.align = align&lt;br /&gt;
	self.width = width&lt;br /&gt;
	self.lineSpacing = linespace&lt;br /&gt;
	self.font = font&lt;br /&gt;
	&lt;br /&gt;
	self:setText(text)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function TextWrap:setText(text)&lt;br /&gt;
	self.text = text&lt;br /&gt;
&lt;br /&gt;
	-- remove previous lines&lt;br /&gt;
	local childCount = self:getNumChildren()&lt;br /&gt;
	if childCount &amp;gt; 0 then&lt;br /&gt;
		for i = 0, childCount-1 do&lt;br /&gt;
			self:removeChildAt(childCount-i)&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	-- some calculations&lt;br /&gt;
	local tf = TextField.new(self.font, self.text)&lt;br /&gt;
	tf:setLetterSpacing(self.letterSpacing)&lt;br /&gt;
	local textWidth = tf:getWidth()&lt;br /&gt;
	local letterCount = text:len()&lt;br /&gt;
	local letterChunks = math.floor(letterCount/(textWidth/self.width))&lt;br /&gt;
	local iters = math.ceil(letterCount/letterChunks)&lt;br /&gt;
&lt;br /&gt;
	-- split string in lines&lt;br /&gt;
	local height = 0&lt;br /&gt;
	local last = 1&lt;br /&gt;
	for i = 1, iters do&lt;br /&gt;
		local part = text:sub(last, last+letterChunks-1)&lt;br /&gt;
		local lastSpace = 0&lt;br /&gt;
		local newLine = false&lt;br /&gt;
		local len = part:len()&lt;br /&gt;
		local startStr = part:find(&amp;quot;\n&amp;quot;)&lt;br /&gt;
		if startStr ~= nil then&lt;br /&gt;
			lastSpace = startStr - 1&lt;br /&gt;
			last = last + 1&lt;br /&gt;
			newLine = true&lt;br /&gt;
		end&lt;br /&gt;
		if lastSpace == 0 then&lt;br /&gt;
			-- finding last space&lt;br /&gt;
			for i = 1, len do&lt;br /&gt;
				if part:find(&amp;quot; &amp;quot;, -i) ~= nil then&lt;br /&gt;
					lastSpace = ((len-i)+1)&lt;br /&gt;
					break&lt;br /&gt;
				end&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
		if lastSpace &amp;gt; 0 and i ~= iters then&lt;br /&gt;
			last = last + lastSpace&lt;br /&gt;
			part = part:sub(1, lastSpace)&lt;br /&gt;
		else&lt;br /&gt;
			last = last + letterChunks&lt;br /&gt;
		end&lt;br /&gt;
		local line = TextField.new(self.font, part)&lt;br /&gt;
		line:setLetterSpacing(self.letterSpacing)&lt;br /&gt;
		line:setTextColor(self.textColor)&lt;br /&gt;
		if self.align == &amp;quot;left&amp;quot; or (newLine and self.align == &amp;quot;justify&amp;quot;) then&lt;br /&gt;
			self:addChild(line)&lt;br /&gt;
			line:setPosition(0, height)&lt;br /&gt;
		elseif self.align == &amp;quot;right&amp;quot; then&lt;br /&gt;
			self:addChild(line)&lt;br /&gt;
			line:setPosition(self.width - line:getWidth(), height)&lt;br /&gt;
		elseif self.align == &amp;quot;center&amp;quot; then&lt;br /&gt;
			self:addChild(line)&lt;br /&gt;
			line:setPosition((self.width - line:getWidth())/2, height)&lt;br /&gt;
		elseif self.align == &amp;quot;justify&amp;quot; then&lt;br /&gt;
			local diff = self.width - line:getWidth()&lt;br /&gt;
			-- if no difference or last line&lt;br /&gt;
			if diff == 0 or i == iters then&lt;br /&gt;
				self:addChild(line)&lt;br /&gt;
				line:setPosition(0, height)&lt;br /&gt;
			else&lt;br /&gt;
				local res, spaceCount = part:gsub(&amp;quot; &amp;quot;, &amp;quot;&amp;quot;)&lt;br /&gt;
				newLine = TextField.new(self.font, res)&lt;br /&gt;
				newLine:setLetterSpacing(self.letterSpacing)&lt;br /&gt;
				diff = self.width - newLine:getWidth()&lt;br /&gt;
				local eachSpace = diff/(spaceCount-1)&lt;br /&gt;
				local lastPos = 0&lt;br /&gt;
				for wordString in part:gmatch(&amp;quot;[^%s]+&amp;quot;) do&lt;br /&gt;
					local word = TextField.new(self.font, wordString)&lt;br /&gt;
					word:setLetterSpacing(self.letterSpacing)&lt;br /&gt;
					word:setTextColor(self.textColor)&lt;br /&gt;
					self:addChild(word)&lt;br /&gt;
					word:setPosition(lastPos, height)&lt;br /&gt;
					lastPos = lastPos + word:getWidth() + eachSpace&lt;br /&gt;
				end&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
		height = height + line:getHeight() + self.lineSpacing&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function TextWrap:getText()&lt;br /&gt;
	return self.text&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function TextWrap:setTextColor(color)&lt;br /&gt;
	self.textColor = color&lt;br /&gt;
	for i = 1, self:getNumChildren() do&lt;br /&gt;
		local sprite = self:getChildAt(i)&lt;br /&gt;
		sprite:setTextColor(color)&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function TextWrap:getTextColor()&lt;br /&gt;
	return self.textColor&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function TextWrap:setLetterSpacing(spacing)&lt;br /&gt;
	self.letterSpacing = spacing&lt;br /&gt;
	for i = 1, self:getNumChildren() do&lt;br /&gt;
		local sprite = self:getChildAt(i)&lt;br /&gt;
		sprite:setLetterSpacing(spacing)&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function TextWrap:getLetterSpacing()&lt;br /&gt;
	return self.letterSpacing&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function TextWrap:setAlignment(align)&lt;br /&gt;
	self.align = align&lt;br /&gt;
	self:setText(self.text)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function TextWrap:getAlignment()&lt;br /&gt;
	return self.align&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function TextWrap:setWidth(width)&lt;br /&gt;
	self.width = width&lt;br /&gt;
	self:setText(self.text)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function TextWrap:getWidth()&lt;br /&gt;
	return self.width&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function TextWrap:setLineSpacing(linespace)&lt;br /&gt;
	self.lineSpacing = linespace&lt;br /&gt;
	self:setText(self.text)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function TextWrap:getLineSpacing()&lt;br /&gt;
	return self.lineSpacing&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function TextWrap:setFont(font)&lt;br /&gt;
	self.font = font&lt;br /&gt;
	self:setText(self.text)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function TextWrap:getFont()&lt;br /&gt;
	return self.font&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Text Wrap2 @ar2rsawseen &amp;#039;&amp;#039;&amp;#039;Demo&amp;#039;&amp;#039;&amp;#039; ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
application:setBackgroundColor(0x6c6c6c)&lt;br /&gt;
&lt;br /&gt;
-- some vars&lt;br /&gt;
local width = application:getContentWidth()&lt;br /&gt;
local fonttitle = TTFont.new(&amp;quot;fonts/tahoma.ttf&amp;quot;, 32)&lt;br /&gt;
local font = TTFont.new(&amp;quot;fonts/tahoma.ttf&amp;quot;, 22)&lt;br /&gt;
&lt;br /&gt;
-- main title&lt;br /&gt;
local maintitle = TextWrap.new(&amp;quot;TextWrap&amp;quot;, width, nil, 0.6, fonttitle)&lt;br /&gt;
maintitle:setLetterSpacing(8*3)&lt;br /&gt;
maintitle:setAlignment(&amp;quot;center&amp;quot;)&lt;br /&gt;
maintitle:setTextColor(0x66aaff)&lt;br /&gt;
&lt;br /&gt;
-- left side&lt;br /&gt;
local ltext =&lt;br /&gt;
	&amp;quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc suscipit arcu placerat lorem iaculis bibendum.&amp;quot;..&lt;br /&gt;
	&amp;quot;Phasellus eu urna non massa rutrum euismod. Proin ac scelerisque augue. Sed vitae augue mi. Cras mi tellus, auctor pulvinar&amp;quot;..&lt;br /&gt;
	&amp;quot;aliquet sed, suscipit eget justo. Nulla nec sem ac metus mollis ullamcorper eget eget dolor.&amp;quot;..&lt;br /&gt;
	&amp;quot;In vitae metus quis ligula mollis blandit. Vestibulum est sem, ultricies id tempus et, sollicitudin eu augue.&amp;quot;&lt;br /&gt;
local ltw = TextWrap.new(ltext, width/2-8, &amp;quot;left&amp;quot;, 0.6, font)&lt;br /&gt;
&lt;br /&gt;
-- right side&lt;br /&gt;
-- title&lt;br /&gt;
local rtwtitle = TextWrap.new(&amp;quot;Title&amp;quot;, width/2-8*2, &amp;quot;center&amp;quot;, 0.6, font)&lt;br /&gt;
rtwtitle:setLetterSpacing(8*1)&lt;br /&gt;
rtwtitle:setTextColor(0xffaa66)&lt;br /&gt;
-- text&lt;br /&gt;
local rtw = TextWrap.new(&amp;quot;Description: Lorem ipsum dolor sit amet, consectetur adipiscing elit.&amp;quot;, width/2-8)&lt;br /&gt;
rtw:setAlignment(&amp;quot;justify&amp;quot;)&lt;br /&gt;
rtw:setLineSpacing(0.6)&lt;br /&gt;
rtw:setFont(font)&lt;br /&gt;
rtw:setTextColor(0xaaff66)&lt;br /&gt;
&lt;br /&gt;
-- position&lt;br /&gt;
maintitle:setPosition(0, 32)&lt;br /&gt;
ltw:setPosition(0, 32*3) -- left side&lt;br /&gt;
rtwtitle:setPosition(width/2, 32*3) -- right side&lt;br /&gt;
rtw:setPosition(width/2, 32*4) -- right side&lt;br /&gt;
-- order&lt;br /&gt;
stage:addChild(maintitle)&lt;br /&gt;
stage:addChild(ltw) -- left side&lt;br /&gt;
stage:addChild(rtwtitle) -- right side&lt;br /&gt;
stage:addChild(rtw) -- right side&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;[[UI Text]]&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
{{GIDEROS IMPORTANT LINKS}}&lt;/div&gt;</summary>
		<author><name>MoKaLux</name></author>
	</entry>
</feed>