textbox.lua (a simple textfield for Corona SDK that works on Simulator)
Download here: textbox.lua with sample code
Version 1.0, 2001-03-09
by Rodrigo Souza (rodrigorgsATgmail.com), from RoDen Apps
This is a pure Lua implementation of a textfield that works on the
Simulator. It's intended to be used for debugging purposes, as it is
too ugly to be presented to final users. Also, it doesn't handle
strings that don't fit the texbox. No copy&paste either.
Feel free to use it as you wish, but please keep the original credits.
Sample code with instructions:
---------------------------------------------------------------------
-- Loading this library
---------------------------------------------------------------------
local textbox = require("textbox")
---------------------------------------------------------------------
-- Creating a textbox
---------------------------------------------------------------------
local x, y = 20, 40
local width, height = 280, 30
local fontSize = 16
local mytext = textbox.newTextbox("Hello World", x, y, width, height, fontSize)
---------------------------------------------------------------------
-- Changing textbox contents
---------------------------------------------------------------------
mytext:setText("Hi") -- change contents to "Hi"
mytext:getText() -- returns contents ("Hi")
mytext:append("!!") -- appends a string to the end of the textbox
mytext:backspace() -- removes the last character from the textbox
---------------------------------------------------------------------
-- Connecting a button to a textbox (uses ui.lua)
---------------------------------------------------------------------
local ui = require("ui")
local button = ui.newButton {
default = "buttonBlue.png",
over = "buttonBlueOver.png",
text = "Write X",
x = display.contentWidth / 2,
y = 180,
onPress = textbox.handleButtonPress
}
button.char = "X"
-- The relevant lines here are:
-- onPress = textbox.handleButtonPress
-- and
-- button.char = "X"
--
-- button.char is the character that is appended to the textbox
-- when the button is pressed. If button.char is "\8", then the
-- last character of the textbox is removed when the button is
-- pressed.
--
-- textbox.handleButtonPress, which is called when the button is
-- pressed, is a function that appends the button's .char to the
-- active textbox (the textbox that is retaining the focus).
-- To give focus to a textbox, just tap on it.
---------------------------------------------------------------------
-- Handling focus
---------------------------------------------------------------------
mytext:focus() -- sets focus to mytext
textbox.getActive() -- returns the textbox that has focus (mytext)
textbox.removeFocus() -- removes focus from whathever textbox has focus
-- You can declare functions that are called when the focus changes
function gainFocus(txtbox) print("gain: " .. txtbox:getText()) end
function loseFocus(txtbox) print("lose: " .. txtbox:getText()) end
mytext.onGainFocus = gainFocus -- function to call when mytext gains focus
mytext.onLoose = loseFocus -- function to call when mytext loses focus