Game Development Reference
In-Depth Information
Text Attributes
A
MOAITextBox
can display text, which can be altered using the
setString
function. Note that the text
can include embedded codes that alter the color of the text, similar to HTML tags:
textbox:setString("This text is in <c:ff0000>Red<c>, while this one is <c:00ff00>Green<c>.")
The tag to change the color starts with
<c:
xxxxxx
>
and ends with
<c>
. The
xxxxxx
is the color in hex
(in the RRGGBBAA format). You can also use
c:
xxx
and
c:
xxxx
for lower-precision color codes.
Text Styles
You can alter the font, size, and color of text. We just discussed using
<c>
tags to change color
and passing font sizes while creating a
MOAIFont
object. If you wanted to create text that requires
different sized fonts in other frameworks, you would create multiple
textbox
objects with different
sizes and then position them accordingly. However, with Moai, you can create styles that can be
used for this, as well as for changing the font and color. These styles can be embedded in text in a
manner similar to the color
<c>
tags:
function newStyle(font, size)
local style = MOAITextStyle.new()
style:setFont(font)
style:setSize(size)
return style
end
textbox:setStyle(newStyle(font, 24))
textbox:setStyle( "foo", newStyle(font, 32))
textbox:setStyle("bar", newStyle(font, 48))
text = "This text is <foo>Large</> and this is <bar>Larger</> than the normal text."
textbox:setString(text)
Note
The tags can be nested but must not overlap—that is, a child tag must be closed before closing
a parent tag.
Aligning Text
After creating a
MOAITextBox
, you can change its properties. The text alignment can be altered using
the
setAlignment
function. The parameters passed to
setAlignment
can be one of the following:
MOAITextBox.LEFT_JUSTIFY
MOAITextBox.RIGHT_JUSTIFY
MOAITextBox.CENTER_JUSTIFY