Difference between revisions of "TextField"

From GiderosMobile
m (Text replacement - "</source>" to "</syntaxhighlight>")
Line 37: Line 37:
 
text:setPosition(32, 64)
 
text:setPosition(32, 64)
 
stage:addChild(text)
 
stage:addChild(text)
 +
</syntaxhighlight>
 +
 +
'''Example 5:''' additional text formatting (since Gideros 2024.2):
 +
<syntaxhighlight lang="lua">
 +
local font = TTFont.new("DejaVuSans.ttf", 20, "", nil, nil)
 +
--For l,s and u, the thickness is a fraction of the text line height.
 +
local tf = TextField.new(font, "Text in \e[u=0.2]Underline\e[!u] Text in \e[i=15]Oblique 15°\e[!i] Custom \e[l=0.3:0.2]line placement and \e[color=#f00]thickness\e[color]\e[!l]")
 +
tf:setLayout{w = 400, h = 100, flags = 1280|FontBase.TLF_CENTER|FontBase.TLF_VCENTER}
 +
tf:setPosition(0,40)
 +
stage:addChild(tf)
 
</syntaxhighlight>
 
</syntaxhighlight>
  

Revision as of 17:31, 20 February 2024

Supported platforms: Platform android.pngPlatform ios.pngPlatform mac.pngPlatform pc.pngPlatform html5.pngPlatform winrt.pngPlatform win32.png
Available since: Gideros 2011.6
Inherits from: Sprite

Description

The TextField class is used to create objects for text display.

Examples

Example 1: bitmap font:

local font = Font.new("myfont.txt", "myfont.png") -- you need to add your bitmap font
local textfield = TextField.new(font, "some text")
stage:addChild(textfield)
textfield:setText("some other text") -- change the text

Example 2: default font:

-- to use the default font, pass nil value for the font parameter
local textfield2 = TextField.new(nil, "some text with default font")
stage:addChild(textfield2)

Example 3: TTFont:

local font = TTFont.new("fonts/Tahoma.ttf", 32, "", true, 1) -- you need to add your .ttf font
local text = TextField.new(font, "This is a text")
text:setPosition(100, 100)
stage:addChild(text)

Example 4: colored text:

local text = TextField.new(nil, "This is a \e[color=#f005]semi transparent red\e[color] text")
text:setPosition(32, 64)
stage:addChild(text)

Example 5: additional text formatting (since Gideros 2024.2):

local font = TTFont.new("DejaVuSans.ttf", 20, "", nil, nil)
--For l,s and u, the thickness is a fraction of the text line height. 
local tf = TextField.new(font, "Text in \e[u=0.2]Underline\e[!u] Text in \e[i=15]Oblique 15°\e[!i] Custom \e[l=0.3:0.2]line placement and \e[color=#f00]thickness\e[color]\e[!l]")
tf:setLayout{w = 400, h = 100, flags = 1280|FontBase.TLF_CENTER|FontBase.TLF_VCENTER}
tf:setPosition(0,40)
stage:addChild(tf)

See also

CompositeFont

Methods

TextField.new creates a new TextField object with the specified font and text
TextField:getLayout retrieves the layout parameters of the Textfield
TextField:getLetterSpacing returns the letter-spacing property which is used to increase or decrease the space between characters in a text
TextField:getLineHeight gets the textfield line height
TextField:getPointFromTextPosition returns the coordinates from a given offset within the text
TextField:getSample gets the string used as sample for determining line height
TextField:getText returns the textfield text
TextField:getTextColor returns the textfield text color in hexadecimal format
TextField:getTextPositionFromPoint returns the offset into a text from x,y coordinates
TextField:setFont sets the textfield font to use
TextField:setLayout changes the textfield layout parameters
TextField:setLetterSpacing sets the letter-spacing property which is used to increase or decrease the space between characters in a text
TextField:setSample sets the sample string used for determining the text line height
TextField:setText sets the textfield text to be displayed
TextField:setTextColor sets the textfield text color in hexadecimal format

Events

Constants