livecode.com
  • Home
  • Download
  • Developers
  • Academy
  • Community
  • Store
  • Support
  • About
LiveCode Lessons » How To - LiveCode Mobile Tasks » How do I use Native Text Controls on iOS

Topics

  • Getting Started with Android 3
    • How do I Become an Android Developer on a PC?
    • How do I Become an Android Developer on a Mac?
    • The Basics: How do I Create Hello World on Android?
  • Getting Started with iOS 2
    • How do I Become an iOS Developer?
    • How do I use Core Location in revMobile
  • LiveCode Mobile Tasks 22
    • How do I Develop Cross-Platform in LiveCode?
    • How do I send an email from my mobile device?
    • How do I use multi-touch to move more than one object?
    • How do I use the Question and Password Dialogues in LiveCode Mobile?
    • How do I Capture Images in LiveCode Mobile?
    • How do I detect a shaking motion using LiveCode mobile?
    • LiveCode Mobile Video How-To's - Shake
    • How do I implement a multi-touch pinch motion?
    • LiveCode Mobile Video How-To's - Multi-touch
    • How do I play a video in part of the screen in iOS
    • Displaying Assets On Differing Screen Resolutions
    • How to create and use an SQLite database
    • How do I get an image from my mobile photo library
    • How do I implement in-app purchases in LiveCode?
    • Accessing Facebook Api's using LiveCode
    • Mobile Orientations
    • Using local notifications
    • Using custom URL schemes
    • Using multi-channel audio on mobile
    • How do I get the Location and use the Digital Compass?
    • How do I use Ads in LiveCode?
    • Creating a native scroller to scroll a field
  • iOS Tasks 19
    • How do I build an iPhone application for iOS?
    • How do I Configure the Status Bar in iOS?
    • How do I make a phone call on the iPhone?
    • How do I use Native Text Controls on iOS
    • How do I use the Browser Control?
    • How do I read/write to files in iOS?
    • How do I Send HTML E-Mails with Attachments in iOS?
    • Installing custom fonts on iOS
    • How do I use the Question and Password Dialogues in iOS?
    • How do I play sounds on an iOS device?
    • How do I use the Picker View on an iPhone?
    • How do I Access Maps on iOS?
    • Creating a simple stock control application for the iPad
    • How do I Create a Distribution Profile for iOS?
    • How do I Submit an iOS App to the App Store?
    • How do I set up an App for Submission to iTunes Connect?
    • How do I use the Date Picker View on an iPhone?
    • How do I use Push Notifications with iOS?
    • How do I Develop iOS Code for Standard and Retina Devices?
  • Android Tasks 4
    • Using the hardware "back" button on Android
    • How do I Create an Android App for Distribution?
    • How do I Create a Self-Signed Certificate for an Android App?
    • How do I use Push Notifications with Android?

Last Updated

Sep 07, 2011

Other Resources

  • Getting Started with LiveCode

  • Get Up and Running with LiveCode
  • Getting Started with LiveCode Development
  • LiveCode Concepts

  • Features, concepts and aspects of LiveCode
  • LiveCode Lessons

  • How To - Step-By-Step Guides To Tasks In LiveCode
  • How To - LiveCode Server Tasks
  • How To - LiveCode Mobile Tasks
  • How To - LiveCode Sample Scripts
  • How to - LiveCode Marketplace Products
  • How to Purchase and License LiveCode
  • Tutorials

  • Creating a Video Library Application
  • Data Grid

  • LiveCode Data Grid
  • Data Grid Tips & Tricks
  • Converting the Stock Program

Comments

6 for this lesson

  • Prev: How do I make a phone call on the iPhone?
  • Next: How do I use the Browser Control?

How do I use Native Text Controls on iOS

This lesson describes how to interact with the native UITextField class from within your LiveCode application.
Screen captures and sample code are provided.

Attached Files

  • NativeControl.livecode

Introduction

The UITextField class within iOS provides a one line editable text field. This text field can be used to collect small amounts of data from users. In addition to basic text editing, the UITextField supports a range of text formatting and keyboard types.
This lesson shows you how to interact with the the UITextField class from within your LiveCode application and demonstrates some of the features that are available. You should review the Release Notes that accompany your release of LiveCode with iOS support in order to get updates on the features covered here.

Creating a Test Interface

The interface shown in this step provides a number of option menus that allow you to set properties that control how you interact with the native text field. You can control font size, border style, auto correction type, keyboard type, content type, text color, text align, auto capitalization, keyboard style, return key type and receive the editing, text changed and return key messages. This list is only a subset of the native text field control features that are available in LiveCode. Refer to the relevant LiveCode with iOS support product documentation for more information on what additional features are supported.

The option menus do not contain any LiveCode script and are processed purely on the content of their names and labels. A generic message handler probes the content of these values for each option menu and applies the value using the command iphoneControlSet.

Setting up the Button Code

The stack in this lesson has three buttons: The Reset Text Changed button, the Create button and the Delete button. Each of these buttons executes a small script. These scripts are listed in this step.

The Reset Text Changed button changes label "Text Changed: true" to "Text Changed: false"

on mouseUp
put "Text Changed: false" into field "textChanged"
end mouseUp

The Create button creates an instance of a text field.

on mouseUp
    inputCreate
end mouseUp

The Delete button deletes an instance of a text field.

on mouseUp
    inputDelete
end mouseUp

Setting up the Stack Code

The card stack contains a number of message handlers that are responsible for a number of control aspects within the application. The comments in the following message event handler scripts provide details of what each script does.

on closeCard
    # handle the close card event
    inputDelete
end closeCard

on inputCreate
    # handle the input creation event
    if "testinput" is among the lines of iphoneControls() then
        # ensure that only one native text input is open
        inputDelete
    end if
    iphoneControlCreate "input", "testinput"
    # set the native text input field to the size and position of the graphic input rectangle
    iphoneControlSet "testinput", "rect", the rect of graphic "InputRect"
    iphoneControlSet "testinput", "visible", true
    # make sure the selected option menu options are applied to the native text input
    updateFieldProps
end inputCreate

on inputDelete
    # delete the native text input
    iphoneControlDelete "testinput"
end inputDelete

on inputFocus
    # set the focus to the native text input
    iphoneControlDo "testinput", "focus"
end inputFocus

on mouseUp
    # handle any mouseUp here that is not handled explicitly by a control
    if word 1 of the name of the target is "button" then
        updateFieldProps
    end if
end mouseUp

on inputBeginEditing
    # handle the inputBeingEdited message
    # this updates a label field on the interface
    put "Editing: true" into field "editing"
end inputBeginEditing

on inputEndEditing
    # handle the inputEndEdited message
    # this updates a label field on the interface
    put "Editing: false" into field "editing"
end inputEndEditing

on inputTextChanged
    # handle the inputTextChanged message
    # this updates a label field on the interface
    put "Text Changed: true" into field "textChanged"
end inputTextChanged

on inputReturnKey
    # handle the inputReturnKey message
    # this raises a dialog box, indicating that the return key was pressed
    # and focus was removed from the text field
    answer "The return key was pressed" with "Okay"
end inputReturnKey

Updating the Properties

As discussed earlier in this lesson, the option menu items do not have LiveCode associated with them. Rather, they are processed using a generic message handler that uses the name and the label of the option menus to set up the respective control that has been changed. updateFieldProps is stored on the stack script and implements this generic process.

on updateFieldProps
    local tX
    # ensure that we are handling messages from our native text input
    if "testinput" is not among the lines of iphoneControls() then
        exit updateFieldProps
    end if
    # loop through each control on the card
    repeat with tX = 1 to the number of controls
        # if the control name does not start with "property" then go to the next item
        if word 1 of the short name of control tX is not "property" then
            next repeat
        end if
        # set the native control property using the value of the current card control
        iphoneControlSet "testinput", word 2 of the short name of control tX, the label of control tX
    end repeat
end updateFieldProps

Note: Using a generic process to apply any option menu selection allows you as a developer to add or remove option menus, knowing that any further LiveCode changes are minimal or not needed at all.

Editing Example 1

Once you raise your application on the iOS device of choice, you should be presented with the following or similar display. You have to select the button Create before you can start entering text. Once text has been entered (see next example), you should see how the label content of Text Changed and Editing changes. You can reset the Text Changed label by selecting the Reset Text Changed button. Selecting the option menus and altering their content should have an immediate impact on the content that is displayed in the text field.

Editing Example 2

You can enter text by selecting the text input area. This raises the keyboard you specified under the Keyboard Style option menu. Type in the text as required and complete editing with the return key.

Editing Example 3 - Multline text input

You can also create native multiline text input controls on iOS. The multiline input control allows the editing of multiple lines of text, with the 'return' key ending each line.

The process is exactly as described about but you create a control of type 'multiline' in the inputCreate handler.

on inputCreate
    # handle the input creation event
    if "testinput" is among the lines of iphoneControls() then
        # ensure that only one native text input is open
        inputDelete
    end if
    iphoneControlCreate "multiline", "testinput"
    # set the native text input field to the size and position of the graphic input rectangle
    iphoneControlSet "testinput", "rect", the rect of graphic "InputRect"
    iphoneControlSet "testinput", "visible", true
    # make sure the selected option menu options are applied to the native text input
    updateFieldProps
end inputCreate

  • Prev: How do I make a phone call on the iPhone?
  • Next: How do I use the Browser Control?

Comments (6)

Daniel ROBERT Monday Jan 30 at 03:23 AM

Hello, very interesting !
Is it possible to find this stack already done ?

Thank-you
Best regards
Daniel ROBERT

Hanson Schmidt-Cornelius Wednesday Feb 01 at 08:42 PM

Hi Daniel,

I have uploaded the sample stack to this lesson.

Kind Regards,

Hanson

Matthias Rebbe Sunday Mar 18 at 10:25 AM

Hi,

the autoCapitalization does not work. The name for this text input property is autoCapitalizationType and not just autoCapitalization.

So to get autoCapitalization working you have to rename the btn "property autoCapitalization" to "property autoCapitalizationType"

Btw: the dictionary also lists the wrong property name.
Regards,

Matthias

Hanson Schmidt-Cornelius Monday Mar 19 at 09:13 PM

Hi Matthias,

thank you very much for your comment. The test stack has been updated and the dictionary entry fixed for the next release.

Kind Regards,

Hanson

Danny Friday Mar 23 at 07:46 AM

Hi

Thanks for this - very helpful, however ... I downloaded the sample stack and when I want to open it, Livecode gives an error message: "there was a problem opening the stack: file is not a stack". Is the file to download corrupted in some way?

Regards
Danny

Hanson Schmidt-Cornelius Saturday Mar 24 at 12:55 AM

Hi Danny,

no, the stack is not corrupted. As of release LiveCode 5.5.0, the file format is changing to accommodate some of the new features we added.

The features in the sample stack do not take advantage of the features that require the new format, so I uploaded the stack again in the format that is supported with older versions of LiveCode.

You should be able to download and run the stack now.

Kind Regards,

Hanson

Add your comment

E-Mail me when someone replies to this comment
Brought to you by RunRev Ltd, Registered in Scotland, No. SC200728