Hi!
Native textboxes have a lot of little troubles in game apps with fonts, scene changes, etc.
I have coded this custom textboxes with is own custom keyboard for easy text entry in games that not need a complete keyboard.
The code needs TextCandy library for work, but you can easily modify for not need it.
Scroll text inside the text box or limit text length is not implemented yet!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | TextCandy = require("lib_text_candy") TextCandy.AddCharsetFromBMF("FLOREL", "provaFont.fnt", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890}!`?'.,;:()[]{}<>|/@\^$-%+=#_&~*") TextCandy.ScaleCharset("FLOREL", 0.45) TextCandy.AddCharsetFromBMF("KEYB", "keyboardFont.fnt", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890}!?'.,;:()[]{}<>|/@\^$-%+=#_&*") TextCandy.ScaleCharset("KEYB", 0.50) _H=display.contentHeight _W=display.contentWidth local localGroup = display.newGroup() --Define the group for textboxes local textBoxGroup=display.newGroup ( ) localGroup:insert(textBoxGroup) --Define the group for the keyboard key's local teclatGroup =display.newGroup() localGroup:insert(teclatGroup) --Animation for the event key press (requires TextCandy) animacioTeclat={ interval = 1, startNow = true, restartOnChange = true, delay = 0, duration = 100, charWise = true, autoRemoveText = false, frequency = 100, xScaleRange = 2, yScaleRange = 2, } --end of animation --keyboard key's definition (I'use < for backspace and " OK" for enter, no space key is defined) local linea={"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "A", "S", "D", "F", "G", "H", "J", "K", "L", "<", "Z", "X", "C", "V", "B", "N", "M", " OK"} local tecla={} local posicioX=0 local posicioY=0 --fi definicio tecles del teclat --Definition and Creation of textboxes. local textBoxActiu=0 --store the active textbox local textBox={} --store the Rect object of the textbox local texteTextBox={} --store the text object of the textbox local valorTextBox={} --store the real value of the textbox local valorMostratTextBox={} --store the visible value of the textbox (*** for passwords) local variablesTextBox={} --store the textbox's variables we want to create variablesTextBox={ {xRect=40, yRect=100, wRect=240, hRect=40, xText=160, yText=110, password=0}, {xRect=40, yRect=200, wRect=240, hRect=40, xText=160, yText=210, password=1}, } for t=1, #variablesTextBox, 1 do textBox[t]=display.newRect(variablesTextBox[t].xRect, variablesTextBox[t].yRect, variablesTextBox[t].wRect, variablesTextBox[t].hRect ) textBox[t]:setFillColor(255,255,255) textBox[t].index=t textBox[t].isPassword= variablesTextBox[t].password textBoxGroup:insert(textBox[t]) valorMostratTextBox[t]="" valorTextBox[t]="" texteTextBox[t]= TextCandy.CreateText({ fontName = "FLOREL", x= variablesTextBox[t].xText, y= variablesTextBox[t].yText, text= valorTextBox[t], originX = "CENTER", originY = "CENTER", textFlow = "LEFT", charSpacing = 0 }) texteTextBox[t]:setColor(255,20,20) end --end of textbox creation --When touch the textbox set focus on it and show keyboard local function tractaTextBox (event) if event.phase=="began" then for i=1,#textBox, 1 do --First set all textboxes background to standard color textBox[i]: setFillColor (255,255,255) end textBoxActiu=event.target.index textBox[textBoxActiu]: setFillColor (240,240,240) transition.to (teclatGroup, {time=500, y=0}) end end for i=1, #textBox,1 do --adds a touch event handler for every TextBox textBox[i]:addEventListener ( "touch", tractaTextBox) end --Keyboard Creation, first the background, later the keys local fonsTeclat=display.newRect( 0, 320, _W, _H*0.4 ) fonsTeclat:setFillColor ( 0,0,0) teclatGroup:insert(fonsTeclat) for i=1,38,1 do if i<=10 then posicioX= i*32-15 posicioY=340 elseif i>10 and i<=20 then posicioX= (i-10)*32-15 posicioY=380 elseif i>20 and i<=30 then posicioX= (i-20)*32-15 posicioY=420 elseif i>30 and i<=40 then posicioX= (i-30)*32-15 posicioY=460 end tecla[i]= TextCandy.CreateText({ fontName = "KEYB", x= posicioX, y= posicioY, text= linea[i], originX = "CENTER", originY = "CENTER", textFlow = "LEFT", charSpacing = 0 }) tecla[i].valor= linea[i] teclatGroup:insert(tecla[i]) end -- End of keyboard creation --Key press event handler local function tractaTecles(event) if event.phase=="began" then event.target:applyAnimation(animacioTeclat) if event.target.valor=="<" then --backspace handler local llargada=string.len(valorTextBox[textBoxActiu]) if llargada==1 then valorTextBox[textBoxActiu]="" valorMostratTextBox[textBoxActiu]="" texteTextBox[textBoxActiu]:setText(valorMostratTextBox[textBoxActiu]) elseif llargada>1 then valorTextBox[textBoxActiu]=string.sub(valorTextBox[textBoxActiu], 1, llargada-1) if textBox[textBoxActiu].isPassword==1 then valorMostratTextBox[textBoxActiu]="" for n=1, string.len(valorTextBox[textBoxActiu]),1 do valorMostratTextBox[textBoxActiu]=valorMostratTextBox[textBoxActiu].."*" end else valorMostratTextBox[textBoxActiu]=valorTextBox[textBoxActiu] end texteTextBox[textBoxActiu]:setText(valorMostratTextBox[textBoxActiu]) end elseif event.target.valor==" OK" then --OK handler textBox[textBoxActiu]:setFillColor(255,255,255) textBoxActiu=0 transition.to (teclatGroup, {time=500, y=640}) else --other keys handler valorTextBox[textBoxActiu]=valorTextBox[textBoxActiu]..event.target.valor if textBox[textBoxActiu].isPassword==1 then valorMostratTextBox[textBoxActiu]="" for n=1, string.len(valorTextBox[textBoxActiu]),1 do valorMostratTextBox[textBoxActiu]=valorMostratTextBox[textBoxActiu].."*" end else valorMostratTextBox[textBoxActiu]=valorTextBox[textBoxActiu] end texteTextBox[textBoxActiu]:setText(valorMostratTextBox[textBoxActiu]) end end end for i=1,38,1 do tecla[i]:addEventListener ( "touch", tractaTecles ) end --end of key press event handler teclatGroup.y=640 --set the y position of keyboard group out of bounds |
You can find the fonts of this example here:
http://dl.dropbox.com/u/8768831/fontsTextBox.7z
You can find TextCandy library for Corona here:
http://www.x-pressive.com/TextCandy_Corona/download.html
Greetings!
Josua
Hi Josua,
Thanks for the code . just a doubt will it work on both android and iphone devices.