Help with native text fields

Hi,
I have a couple of problems with the native text field objects.

  1. I’m trying to create a placeholder text that gives the user instructions, such as “Search…”. After creating the newTextField i set its text property to “Search…”, which works fine. However, trying to clear that text in the began phase doesn’t work. How can I clear the placeholder text when the user hits the field for the first time?
local fieldHandler = function( event )  
 print( event.phase )  
 if "began" == event.phase then  
 -- do something on "keyboard has appeared" event  
 event.text="";   
  
 elseif "editing" == event.phase then  
 print (event.text);  
  
 elseif "ended" == event.phase then  
 -- This event is called when the user stops editing a field:  
 -- for example, when they touch a different field or keyboard focus goes away  
 -- Hide keyboard  
 native.setKeyboardFocus( nil )  
  
 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  
 local textBox = native.newTextField( 0, 0, 100, 30,fieldHandler )  
  
 textBox:setReferencePoint(display.CenterReferencePoint);  
 textBox.isEditable = true;  
 textBox.text = "Search...";  
  1. What is the best practice to setting the text font for different screen sizes. The textfield looks OK on the iPhone screen but on an iPad, the field is too big for the text. (I’m using 320X480 and letterbox in the config.lua).

Thanks [import]uid: 33608 topic_id: 18483 reply_id: 318483[/import]

Right now, the native objects are somewhat limited. This is because they need to be cross-platform.

For 1) what you really want is a placeholder text feature. I recommend you file a bug report/feature request for that with us.

But as an alternative solution, you could try to draw a Corona display object that looks like a textfield and text on top of it and when it is touched swap out the Corona objects for the real native object.

For 2) I don’t know what the best practice is. But if the aesthetics are really important to you, you might consider doing customized layouts for iPhone and iPad separately. Some people specify different content scaling for iPhone vs. iPad in their config.lua.
[import]uid: 7563 topic_id: 18483 reply_id: 70963[/import]

Thanks. I guess, to implement the placeholder, I’ll wait until the native search box is more mature. [import]uid: 33608 topic_id: 18483 reply_id: 71182[/import]

I just got the placeholder to work. the trick was to create the textbox as a global var.

I still have a problem with the text size on the iPad and wonder if the same issue will happen between different Android resolutions.

Anyway, here is my solution to the placeholder if it helps anyone:

[code]
local fieldHandler = function( event )
print( event.phase )
if “began” == event.phase then
– do something on “keyboard has appeared” event
searchBox.text = “”;
elseif “editing” == event.phase then

elseif “ended” == event.phase then
– This event is called when the user stops editing a field:
– for example, when they touch a different field or keyboard focus goes away
– Hide keyboard
native.setKeyboardFocus( nil )

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
searchBox = native.newTextField( 0, 0, 200, 25,fieldHandler )

searchBox:setReferencePoint(display.CenterReferencePoint);
searchBox.isEditable = true;
searchBox.text = “Search…”;
searchBox.size = 20;
[/code] [import]uid: 33608 topic_id: 18483 reply_id: 71218[/import]

Hey Amir,

Thanks for posting your solution! It’s great when people do that to help others in the future.

Peach :slight_smile: [import]uid: 52491 topic_id: 18483 reply_id: 71231[/import]

Peach,
Anytime. That’s the whole purpose of an online community.
Amir [import]uid: 33608 topic_id: 18483 reply_id: 71357[/import]

Amir,

That is an excellent attitude to have - good for you!

Peach :slight_smile: [import]uid: 52491 topic_id: 18483 reply_id: 71368[/import]