Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Turn off auto-correction on native.newTextBox
---------------------------------------------------------------------------------
-- Example - Turn off auto-correction on native.newTextBox
---------------------------------------------------------------------------------
require "TextBoxNoAutoCorrection"
local textBox = native.newTextBox( display.contentCenterX, display.contentCenterY, display.contentWidth/1.2, 100 )
textBox.noAutoCorrection = true
textBox.isEditable = true
-- -------------------------------------------------------------------------------
-- TextBoxNoAutoCorrection.lua - native.newTextBox extension
-- with option to turn off auto-correction
-- -------------------------------------------------------------------------------
local initialized, prevPosition, len = nil, 0, string.len
if not initialized then
local nativeNewTextBox = native.newTextBox
native.newTextBox = function(...)
local textBox = nativeNewTextBox(...)
textBox:addEventListener("userInput", function(e)
if textBox.noAutoCorrection then
if e.phase == "editing" then
local ett = e.target.text
e.target.text = ett:sub(1,len(ett)-1)
e.target.text = ett
if e.numDeleted > 0 then
if len(e.newCharacters) == 0 then
e.startPosition = e.startPosition-1
textBox:setSelection(e.startPosition, e.startPosition)
else
e.phase = "ignoredAutoCorrectionEvent"
e.startPosition = e.startPosition+e.numDeleted
e.numDeleted, e.newCharacters = 0, ""
end
else
if len(e.newCharacters) == 0 then
e.startPosition = prevPosition
end
textBox:setSelection(e.startPosition, e.startPosition)
end
prevPosition = e.startPosition
end
end
end )
return textBox
end
initialized = true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment