Yep, that will work. The textbox handle will run everytime you enter or change anything in a textBox. It´s a pain to debug under windows/Android as you can´t get text functions to work in the simulator under windows.
But if you have a mac or install hackintosh, you can do efficient debugging under OSX.
Here is a code snippet from one of my programs that shows how it can be handled (you have to change it a little to get it to work):
[lua]
textramme=“Textramme.png”
statText={}
statText[1]=“Player name:”
statText[2]=name
statText[3]=“Player email:”
statText[4]=email
statText[5]=“Password:”
statText[6]=password
statText[7]=“Location:”…string.sub(scorelocation,1,14)
statText[8]=""
statText[9]=“Touch to edit”
statLabel={}
local function textHandle( getObj )
return
function( event )
if ( “began” == event.phase ) then
elseif ( “ended” == event.phase ) or ( “submitted” == event.phase ) or (“editing”== event.phase) then
– This event is called when the user stops editing a field:
– for example, when they touch a different field or keyboard focus goes away
l=getObj().num
statText[l]= tostring( getObj().text )
– print( "Text entered = " … statText[l]) – display the text entered
textreturn=string.find(statText[l],string.char(10),1) – LF er ascii: 10 (ios)
if textreturn~=nil then
– print(“return at:”…textreturn)
– remove “return” from text field (text should not be more than one line)
statText[l]=string.gsub( statText[l], string.char(10), “” )
end
end
end – “return function()”
end
for l=1,#statText do
if l==2 or l==4 or l==6 then
menuText[l] = display.newImageRect( textramme, 600, 75 ) – textramme is my gfx that I use to enclose the textbox
menuText[l].x = display.contentWidth/2
menuText[l].y = 70*l+95
if l==2 or l==4 or l==6 then
textBox[l] = native.newTextBox( 11, 70*l+43, 620, 78)
textBox[l]:addEventListener( ‘userInput’,
textHandle(
function ()
return textBox[l]
end ) )
textBox[l]:setTextColor( 255, 255, 255, 255 )
textBox[l].hasBackground = false
textBox[l].align = “center” – correct alignment/placement only shows up on device or Xcode
textBox[l].text = statText[l]
textBox[l].font = native.newFont( “Cantarell-Bold”, 20 )
textBox[l].inputType = “default”
textBox[l].alpha = 0.5
textBox[l].isEditable = true
textBox[l].num=l
textBox[l].y = 70*l+82
end
end
— Draw the text
statLabel[l]=display.newText(statText[l], display.contentWidth/2+txtpx[k], 70*l+60+txtpy[k], labelFont, 40 )
statLabel[l].x=statLabel[l][k].x-(statLabel[l][k].contentWidth/2)
statLabel[l][k]:setTextColor( 255, 255, 255, 255 )
end
[/lua]
As you can see, this code uses a textbox, but prevents the user from entering a linefeed (LF or Ascii=10) to keep it to a single line. After that you can do about anything with the text to auto-format, move it or whatever.
You may have to adjust the x&y coordinates to superimpose the textbox on the drawn text, but I´ll leave that to you (its easier if you make your text Bold or do some fancy textdrawing).