Last active
August 20, 2018 10:52
-
-
Save jcbnlsn/cd79c7c3aa55b6e2355c to your computer and use it in GitHub Desktop.
Turn off auto-correction on native.newTextBox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--------------------------------------------------------------------------------- | |
-- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- ------------------------------------------------------------------------------- | |
-- 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