Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Corona Invalidation Sample 2 of 2: AutoSizeText.lua. Show's using invalidation to cache all text field changes once per frame.
require "Component"
AutoSizeText = {}
function AutoSizeText:new()
local view = Component:new()
view._text = nil
view.textDirty = false
view._rgba = {255, 255, 255, 255}
view.rgbaDirty = false
view._fontSize = 18
view.fontSizeDirty = false
view._width = nil
view._height = nil
view._autoSize = false
view.autoSizeDirty = false
view._bold = false
function view:createChildren()
self:setText("")
end
function view:getText()
return self._text
end
function view:setText(str, bypass)
if self._text == str and bypass ~= true then
return true
end
self._text = str
self.textDirty = true
self:invalidateProperties()
end
function view:commitProperties()
if self.textDirty == true then
self.textDirty = false
if self.field then
self.field:removeSelf()
self.field = nil
end
local field
local fontName
if self._bold == false then
fontName = native.systemFont
else
fontName = native.systemFontBold
end
if self._autoSize == false then
field = display.newText(str, 0, 0, fontName, self._fontSize)
else
field = display.newText(str, 0, 0, self._width, self._height, fontName, self._fontSize)
end
field:setReferencePoint(display.TopLeftReferencePoint)
field:setTextColor(unpack(self._rgba))
self.field = field
self:insert(field)
end
if self.rgbaDirty == true then
self.field:setTextColor(r, g, b, alpha)
end
end
function view:setTextColor(r, g, b, alpha)
if alpha == nil then
alpha = 255
end
self._rgba = {r, g, b, alpha}
self.rgbaDirty = true
self:invalidateProperties()
end
function view:setFontSize(size)
self._fontSize = size
self.fontSizeDirty = true
self:invalidateProperties()
end
function view:setSize(w, h)
local dirty = false
if self._width ~= w or self._height ~= h then
dirty = true
end
self._width = w
self._height = h
if dirty then
self:setText(self._text, true)
end
end
function view:setAutoSize(val)
if value ~= val then
self._autoSize = val
self:setText(self._text, true)
end
end
function view:setBold(bold)
if value ~= val then
self._bold = bold
self:setText(self._text, true)
end
end
view:init()
return view
end
return AutoSizeText
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment