Difference between revisions of "Article Tutorials/Drawing text with TextField"

From GiderosMobile
(update?)
Line 1: Line 1:
 
== Drawing Text ==
 
== Drawing Text ==
 
+
Type the following code in your main.lua file:
Type the following text into your main.lua file:<br>
+
<source lang="lua">
 
 
<syntaxhighlight lang="lua">
 
 
local textfield = TextField.new(nil, "Hello, world!")
 
local textfield = TextField.new(nil, "Hello, world!")
 
textfield:setX(10)
 
textfield:setX(10)
 
textfield:setY(10)
 
textfield:setY(10)
 
stage:addChild(textfield)
 
stage:addChild(textfield)
</syntaxhighlight><br>
+
</source>
  
Start the Player as shown previously and hit the Play button.<br>
+
Start the Player as shown previously and hit the Play button.
  
[[File:Hello-World-on-the-Player.png|thumb|center]]<br>
+
[[File:Hello-World-on-the-Player.png|thumb|center]]
 
 
Congratulations! You’ve written your first application in Gideros Studio! Pat yourself on the back. Once you’ve finished your celebration, close the Player.<br>
 
 
 
Now, let’s discuss what the code actually does:<br>
 
  
 +
=== Code explanation ===
 
'''local textfield = TextField.new(nil, "Hello, world!")'''<br>
 
'''local textfield = TextField.new(nil, "Hello, world!")'''<br>
 
+
This code creates a local variable “textfield” with the content of “Hello, world!”. The ‘nil’ specifies to use Gideros defa0ult font.
This code creates a local variable “textfield” with the content of “Hello, world!”. The ‘nil’ specifies to use the default font.<br>
 
 
 
  
 
'''textfield:setX(10)'''<br>
 
'''textfield:setX(10)'''<br>
 
 
'''textfield:setY(10)'''<br>
 
'''textfield:setY(10)'''<br>
 
+
These set the X and Y co-ordinates of the top-left corner of the text on the screen to display.
These set the X and Y co-ordinates of the top-left corner of the text on the screen to display.<br>
 
  
 
'''stage:addChild(textfield)'''<br>
 
'''stage:addChild(textfield)'''<br>
 +
This adds the text field to the screen.
  
This adds the text field to the screen.<br>
+
You can change the color of the text played using:<br>
 
+
'''Textfield:setTextColor(0xFF0000)'''<br>
You can change the colour of the text displayed using:<br>
 
 
 
'''Textfield:setTextColor()'''<br>
 
  
An example of this is below. To test it out, add it in your code above “stage:addChild”.<br>
+
== Font ==
 
+
You can use your own font, provided you include it with the application.
'''Textfield:setTextColor(0xff0000)'''<br>
+
<source lang="lua">
 
 
This sets the colour of the above text field to red. To learn more about Hexadecimal colours, you can find more info here:<br>
 
 
 
http://www.color-hex.com/<br><br>
 
 
 
They are the same codes as in HTML. Instead of #ff0000 we use 0xff0000. Quite simple, really.<br>
 
 
 
You can also use your own font, provided you include it with the application.<br>
 
 
 
<syntaxhighlight lang="lua">
 
 
local myfont = Font.new("font.txt", "font.png")
 
local myfont = Font.new("font.txt", "font.png")
</syntaxhighlight><br>
+
</source>
  
The image file “font.png” contains your font.
+
The image file “font.png” contains your font:
  
 
[[File:Font.png|thumb|center]]<br>
 
[[File:Font.png|thumb|center]]<br>
  
<syntaxhighlight lang="lua">
+
<source lang="lua">
 
local textField = TextField.new(myfont, "Hello World")
 
local textField = TextField.new(myfont, "Hello World")
 
stage:addChild(textField)
 
stage:addChild(textField)
</syntaxhighlight><br>
+
</source>
 
 
You can also use any TTF font in your application. Just copy it into Gideros Studio via Add Existing Files:<br>
 
  
[[File:Jason-Oakley-Drawing-Text-TTFfont.png|thumb|center]]<br>
+
== TTFont ==
 +
You can also use any TTF font in your application. Just copy it into Gideros Studio via Add Existing Files:
  
To use this, the following code will do it:<br>
+
[[File:Jason-Oakley-Drawing-Text-TTFfont.png|thumb|center]]
  
<syntaxhighlight lang="lua">
+
Then in your code:
 +
<source lang="lua">
 
local myfont = TTFont.new("LiberationMono-Regular.ttf", 18)
 
local myfont = TTFont.new("LiberationMono-Regular.ttf", 18)
 
local text = TextField.new(myfont, "Hello world")
 
local text = TextField.new(myfont, "Hello world")
 
 
text:setPosition(10, 10)
 
text:setPosition(10, 10)
 
 
stage:addChild(text)
 
stage:addChild(text)
</syntaxhighlight><br>
+
</source>
 
 
The “18” above is the font size. TTF fonts display slowly, so you should pre-cache any characters you will use to display your text. Make sure you cover ALL characters your app will be using.<br>
 
  
 +
The “18” above is the font size.
  
<syntaxhighlight lang="lua">
+
TTF fonts display is slow, so you should pre-cache any characters you will use to display your text. Make sure you cover ALL characters your app will be using:
 +
<source lang="lua">
 
local font = TTFont.new("FtraMd__.ttf", 18, " ABCDEFGHIJKLMNOPQRSTUVWXYZ")
 
local font = TTFont.new("FtraMd__.ttf", 18, " ABCDEFGHIJKLMNOPQRSTUVWXYZ")
 
local text = TextField.new(myfont, "HELLO WORLD")
 
local text = TextField.new(myfont, "HELLO WORLD")
 
 
text:setPosition(10, 10)
 
text:setPosition(10, 10)
 
 
stage:addChild(text)
 
stage:addChild(text)
</syntaxhighlight><br>
+
</source>
  
You can download the assets required by this tutorial from here: [[File:Jason-Oakley-Drawing Text assets.zip|center]]<br><br>
+
You can download the assets required in this tutorial here: [[File:Jason-Oakley-Drawing Text assets.zip|center]]
  
  
'''Note:''' This tutorial was written by Jason Oakley and was originally available here: http://bluebilby.com/2013/04/06/gideros-mobile-tutorial-drawing-text/.
+
'''Note:''' This tutorial was written by Jason Oakley and was originally available here: http://bluebilby.com/2013/04/06/gideros-mobile-tutorial-drawing-text/

Revision as of 22:56, 10 May 2020

Drawing Text

Type the following code in your main.lua file:

local textfield = TextField.new(nil, "Hello, world!")
textfield:setX(10)
textfield:setY(10)
stage:addChild(textfield)

Start the Player as shown previously and hit the Play button.

Hello-World-on-the-Player.png

Code explanation

local textfield = TextField.new(nil, "Hello, world!")
This code creates a local variable “textfield” with the content of “Hello, world!”. The ‘nil’ specifies to use Gideros defa0ult font.

textfield:setX(10)
textfield:setY(10)
These set the X and Y co-ordinates of the top-left corner of the text on the screen to display.

stage:addChild(textfield)
This adds the text field to the screen.

You can change the color of the text played using:
Textfield:setTextColor(0xFF0000)

Font

You can use your own font, provided you include it with the application.

local myfont = Font.new("font.txt", "font.png")

The image file “font.png” contains your font:

Font.png


local textField = TextField.new(myfont, "Hello World")
stage:addChild(textField)

TTFont

You can also use any TTF font in your application. Just copy it into Gideros Studio via Add Existing Files:

Jason-Oakley-Drawing-Text-TTFfont.png

Then in your code:

local myfont = TTFont.new("LiberationMono-Regular.ttf", 18)
local text = TextField.new(myfont, "Hello world")
text:setPosition(10, 10)
stage:addChild(text)

The “18” above is the font size.

TTF fonts display is slow, so you should pre-cache any characters you will use to display your text. Make sure you cover ALL characters your app will be using:

local font = TTFont.new("FtraMd__.ttf", 18, " ABCDEFGHIJKLMNOPQRSTUVWXYZ")
local text = TextField.new(myfont, "HELLO WORLD")
text:setPosition(10, 10)
stage:addChild(text)

You can download the assets required in this tutorial here: File:Jason-Oakley-Drawing Text assets.zip


Note: This tutorial was written by Jason Oakley and was originally available here: http://bluebilby.com/2013/04/06/gideros-mobile-tutorial-drawing-text/