UserGuide

iOS Text Field

From Xojo Documentation

A Text Field control provides a single-line field for entering text. When the user taps in the field, the on-screen keyboard automatically appears. All standard iOS editing features are available, including copy/paste and auto-correct.

Below is a list of commonly used events and properties. Refer to iOSTextField in the Language Reference for the complete list.

Events

TextChange

Called each time the text changes. This means it may be called many times as the user is typing into the field.

Properties

Enabled

A boolean that indicates if the text field is enabled and can have text entered or disabled and cannot have text entered or selected.

KeyboardType

Lets you control the type of on-screen keyboard that is displayed when the user taps in the field. By default the full keyboard is displayed, but there are many other keyboards that are optimized for entering numbers, URLs, phone numbers, emails, etc. You change the type by using the iOSKeyboardTypes enumeration.

Password

A boolean that enables password mode for the field. This means that the text is replaced with dots after each character has been typed.

PlaceHolder

Used to specify placeholder text that is displayed when the field is empty. This text is used to provide instructions or help and displays in a lighter color to distinguish it from text that is actually entered into the field. The text itself is not in the field and does not have to be cleared before the user begins typing.

Text

This is the text that is typed in the field. You can use this property to set the default text that appears or check this property to get the text that has been typed.

TextAlignment, TextColor, TextFont

These properties change the alignment, color and size of the text.

Usage

iOS Text Field

To populate text in a TextField, use the Text propery, which you can set using the Inspector or with code such as this:

NameField.Text = "Bob Roberts"

To get the text that was typed, you also use the Text property. This gets the text in the NameField:

Var userName As Text
userName = NameField.Text

To get the value as it is being changed, use the TextChange event handler and the Text property. This code updates a label with the text being entered in a text field:

NameLabel.Text = Me.Text
iOS Keyboard Decimal Pad

Some types of text fields may contain specific information, so it makes sense to change the keyboard type. For example, if want the user to enter an amount of money, you might want to use the DecimalPad.

You can set the KeyboardType in the Inspector or with code like this:

AmountField.KeyboardType = iOSKeyboardTypes.DecimalPad

A text field always has a border around it but there may be times when you do not want a border. This code in the Open event handler for the text field removes the border:

Declare Sub setBorderStyle Lib "UIKit" Selector "setBorderStyle:" (id As Ptr, style As Integer)
setborderStyle(Ptr(Me.Handle), 0)

Example Projects

  • Examples/iOS/Declares/TextFieldBorder

See Also

iOSTextField class; UserGuide:iOS UI, UserGuide:iOS Text Area topics