Skip to content

Instantly share code, notes, and snippets.

@Vavius
Created August 1, 2013 16:04
Show Gist options
  • Save Vavius/6132784 to your computer and use it in GitHub Desktop.
Save Vavius/6132784 to your computer and use it in GitHub Desktop.
Workaround for blurry text in MOAI when Viewport scale is different from device screen resolution.
-- Label will create a bigger sized font and scale it down
-- if view size is less than screen size.
function Label:init(text, width, height, font, textSize)
DisplayObject.init(self)
local scale = math.min(M.getContentScale())
-- do not apply scale fix to Bitmap fonts
if type(font) == "string" then
local fileBase, fileExt = Resources.extractFileExtension(font)
if fileExt == ".fnt" then scale = 1 end
elseif font and font.isBitmap then
scale = 1
end
local textSize = textSize or Font.DEFAULT_POINTS
textSize = textSize * scale
font = Resources.getFont(font, nil, textSize)
self._scaleFix = scale
self:setScl(1 / scale)
self:setFont(font)
self:setSize(width or 10, height or 10)
self:setTextSize(textSize or Font.DEFAULT_POINTS)
self:setString(text)
if not width then
self:fitSize(#text)
elseif not height then
self:fitHeight(#text)
end
-- replace some default methods to hide scaling fix
self._setScl = self.setScl
self.setScl = function(self, xScl, yScl, zScl)
xScl = xScl or 1
self:_setScl(xScl / scale, (yScl or xScl) / scale, (zScl or 1) / scale)
end
self._getScl = self.getScl
self.getScl = function(self)
local xScl, yScl, zScl = self:_getScl()
return xScl * scale, yScl * scale, zScl * scale
end
self._setTextSize = self.setTextSize
self.setTextSize = function(self, size)
self:_setTextSize(size * self._scaleFix)
end
end
--------------------------------------------------------------------------------
-- Sets the size.
-- @param width Width
-- @param height Height
--------------------------------------------------------------------------------
function Label:setSize(width, height)
self:setRect(0, 0, width * self._scaleFix, height * self._scaleFix)
end
--------------------------------------------------------------------------------
-- Sets the fit size.
-- @param lenfth (Option)Length of the text.
-- @param maxWidth (Option)maxWidth of the text.
-- @param maxHeight (Option)maxHeight of the text.
-- @param padding (Option)padding of the text.
--------------------------------------------------------------------------------
function Label:getContentSize()
local w, h = self:getSize()
return w / self._scaleFix, h / self._scaleFix
end
function Label:getContentWidth()
local w, h = self:getContentSize()
return w
end
function Label:getContentHeight()
local w, h = self:getContentSize()
return h
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment