Hey guys, I’m working on a multiline text input workaround for an app that I’m trying to finish up. Basically, I thought I could hide a native text field and set the keyboard focus to it, then use an enterFrame listener to update a textBox object with the text from the textField object. I’ve got a good start, but it’s becoming clear that it won’t be as good as a regular iPhone input text field without some event tracking for the textBox objects. Here’s my code:
local ui = require("ui")
local bkgd = display.newRect( 0, 0, display.contentWidth, display.contentHeight )
bkgd:setFillColor( 50, 50, 75, 255 )
-------------------------------------------
-- \*\*\* handler for native keyboard \*\*\*
-------------------------------------------
local function fieldHandler( event )
if ( "began" == event.phase ) then
-- This is the "keyboard has appeared" event
-- In some cases you may want to adjust the interface when the keyboard appears.
elseif ( "ended" == event.phase ) then
-- This event is called when the user stops editing a field: for example, when they touch a different field
elseif ( "submitted" == event.phase ) then
-- This event occurs when the user presses the "return" key (if available) on the onscreen keyboard
-- Hide keyboard
native.setKeyboardFocus( nil )
end
end
-- create text objects
local textField = native.newTextField( 20, display.contentHeight+50, display.contentWidth-20, 30, fieldHandler )
textField.font = native.newFont( native.systemFontBold, inputFontSize )
local textBox
textBox = native.newTextBox( 0, 20, display.contentWidth, display.contentHeight / 2 )
-- update textBox to match textField text
function updateText( event )
textBox.text=textField.text
end
Runtime:addEventListener( "enterFrame", updateText )
-- add listener to open keyboard on stage touch
local overlayListener = function( event )
native.setKeyboardFocus( textField )
end
Runtime:addEventListener( "tap", overlayListener )
native.setKeyboardFocus( textField )
So here are the problems that I need help with (if they are even possible):
-
- How do I set a tap listener on top of the textBox? It is set for the whole stage, but it only works when I tap the background outside of the textBox. As is, when you are finished editing the text you would have to tap a button or something outside of the textBox in order to continue editing, which is not intuitive.
-
- Is there a way of tracking the cursor select feature of the textBox? If so, we might be able to offset the text in the textField so that the cursor can be dropped in the same place for editing? I think this is a long shot, but I’m hoping you guys might have an idea. Otherwise, the only option is to backspace like crazy if you need to edit something that you typed several lines up.
-
- Is there a way to turn off the auto-correct spelling feature? As is, the spelling suggestions are hidden along with the textField, so when the iPhone thinks you have misspelled something it just replaces it without giving you the option to cancel out. I can live without using the auto-correct feature, but it would be nice to turn it off so that words don’t get replaced without prompting.
I think that’s it. I really hope that Corona has better support for form and data features in the future, if so I could see myself being a subscriber for years and years to come!
Thanks
-Steven [import]uid: 40611 topic_id: 7868 reply_id: 307868[/import]