Hello everyone,
I was wondering, if anyone knows a good way how to limit a user in including too much characters in TextField.
I am trying to use a listener function, where I am controlling the length of inserted string so far. When I detect limit length, I would like to deny TextField from appending new characters. But I am not able to do it. New characters are inserted everytime. (user can either type or paste text from clipboard). Here is some code:
local maxChars = 100
local function inputEventHandler(event)
if event.phase == "editing" then
if event.oldString:len() \>= maxChars and event.newCharacters:len() \> 0 then
-- length of oldString is maximal possible and inserting
-- of new characters is detected - lets cut the rest
-- Unfortunately, textField.text does not contain new characters yet, so this does not help.
textField.text = event.target.text:sub(1, maxChars)
event.newCharacters = "" -- this is not working of course, but I tried it :)
return true -- this also does not prevent TextField from inserting new chars
end
end
end
textField = native.newTextField( 100, 100, 100, 30, inputEventHandler)
This works not wery well, new characters are always appended, which is not what I want.
Any suggestion will be very appreciated. Thank you.
Radim [import]uid: 52745 topic_id: 12345 reply_id: 312345[/import]
[import]uid: 12482 topic_id: 12345 reply_id: 66602[/import]