iOS Developer Library

Developer

UIKit Framework Reference UITextField Class Reference

Options
Deployment Target:

On This Page
Language:

UITextField

A UITextField object displays an editable text area in your interface. You use text fields to gather text-based input from the user using the onscreen keyboard. The keyboard is configurable for many different types of input such as plain text, emails, numbers, and so on. Text fields use the target-action mechanism and a delegate object to report changes made during the course of editing.

In addition to its basic text-editing behavior, you can add overlay views to a text field to display additional information and provide additional tappable controls. You might add custom overlay views for elements such as a bookmarks button or search icon. Text fields provide a built-in overlay view to clear the current text. The use of custom overlay views is optional.

image: ../Art/uitextfield_intro_2x.png

After adding a text field to your interface, you configure it for use in your app. Configuration involves performing some or all of the following tasks:

  • Configure one or more targets and actions for the text field.

  • Configure the keyboard-related attributes of the text field.

  • Assign a delegate object to handle important tasks, such as:

    • Determining whether the user should be allowed to edit the text field’s contents.

    • Validating the text entered by the user.

    • Responding to taps in the keyboard’s return button.

    • Forwarding the user-entered text to other parts of your app.

  • Store a reference to the text field in one of your controller objects.

For information about the methods of the text field’s delegate object, see UITextFieldDelegate Protocol Reference.

Showing and Hiding the Keyboard

When a text field becomes first responder, the system automatically shows the keyboard and binds its input to the text field. A text field becomes the first responder automatically when the user taps it. You can also force a text field to become the first responder by calling its becomeFirstResponder method. You might force a text field to become first responder when you require the user to enter some information.

You can ask the system to dismiss the keyboard by calling the resignFirstResponder method of your text field. Usually, you dismiss the keyboard in response to specific interactions. For example, you might dismiss the keyboard when the user taps the keyboard’s return key. The system can also dismiss the keyboard in response to user actions. Specifically, the system dismisses the keyboard when the user taps a new control that does not support keyboard input.

The appearance and dismissal of the keyboard affect the editing state of the text field. When the keyboard appears, the text field enters the editing state and sends the appropriate notifications to its delegate. Similarly, when the text field resigns the first responder status, it leaves the editing state and notifies its delegate. For more information about the sequence of events that occur during editing, see Understanding the Delegate Interactions with a Text Field.

Configuring the Keyboard’s Appearance

You customize your text field’s keyboard using the properties of the UITextInputTraits protocol, which the UITextField class adopts. UIKit supports standard keyboards for the user’s current language and also supports specialized keyboards for entering numbers, URLs, email addresses, and other specific types of information. You use the properties of this protocol to adjust keyboard traits such as the following:

  • The type of keyboard to display

  • The autocapitalization behavior of the keyboard

  • The autocorrection behavior of the keyboard

  • The type of return key to display

For information about how to configure keyboard attributes in Interface Builder or in your code, see Table 2.

Responding to Keyboard-Related Notifications

Because the system manages the showing and hiding of the keyboard in response to responder changes, it posts the following notifications for tracking the keyboard-related changes:

Each notification contains a userInfo dictionary that includes the size of the keyboard. Because the keyboard can hide portions of your interface, you should use the size information to reposition your content on the screen. For content embedded in a scroll view, you can scroll the text field into view, as illustrated in Figure 1. In other cases, you can resize your main content view so that it is not covered by the keyboard.

Figure 1Adjusting the position of a text field when the keyboard appears image: ../Art/keyboard_adjust_2x.png

For more information about managing keyboard interactions, see Text Programming Guide for iOS.

Formatting the Text in a Text Field

There are two types of formatting you can do to a text field’s text:

  • You can change the font, color, and style of the text using properties of this class. Alternatively, you can specify an NSAttributedString for the text field’s content.

  • You can format the content of a text field using an NSFormatter object.

The font, textColor, and textAlignment properties, among others, affect the appearance of the text field’s string. Modifying these properties applies the specified characteristic to the entire string. To specify more granular formatting, specify the text field’s text using an NSAttributedString object.

The UITextField class does not provide built-in support for formatting its string using an NSFormatter object, but you can use the text field’s delegate to format the content yourself. To do so, use the text field’s delegate methods to validate text and to format it appropriately. For example, use the textField:shouldChangeCharactersInRange:replacementString: method to validate and format text while the user is typing. For information about how to use formatter objects, see Data Formatting Guide.

Using Overlay Views to Edit Content

Overlay views are small views displayed on the left and right sides of the text view’s editable area. Typically, overlay views are image-based buttons that you set up as additional editing controls. For example, you might use an overlay view to implement a bookmarks button. To configure a button as an overlay view, specify an image for the button’s content and configure the target and action of the button to respond to taps.

Listing 1 shows how to add a button as the left overlay of a text field. In this case, the code creates a button and configure its size and contents. The leftViewMode property specifies when your button is displayed. When the user taps the button, the button calls the configured action method, which in this case is a custom displayBookmarks: method.

Listing 1Adding an overlay view to a text field
  1. UIButton* overlayButton = [UIButton buttonWithType:UIButtonTypeCustom];
  2. [overlayButton setImage:[UIImage imageNamed:@"bookmark"] forState:UIControlStateNormal];
  3. [overlayButton addTarget:self action:@selector(displayBookmarks:)
  4. forControlEvents:UIControlEventTouchUpInside];
  5. overlayButton = CGRectMake(0, 0, 28, 28);
  6. // Assign the overlay button to a stored text field
  7. self.textField.leftView = overlayButton;
  8. self.textField.leftViewMode = UITextFieldViewModeAlways;

When configuring overlay views, consider whether you want your text field to display the built-in clear button. The clear button provides the user with a convenient way to delete all of the text field’s text. This button is displayed in the right overlay position, but if you provide a custom right overlay view, use the rightViewMode and clearButtonMode properties to define when your custom overlay should be displayed and when the clear button should be displayed.

Validating Text and Managing the Editing Process

A text field manages the editing of its text with the help of its delegate object. As the user interacts with a text field, the text field notifies its delegate and gives it a chance to control what is happening. You can use the delegate methods to prevent the user from starting or stopping the editing process or to validate text as it is typed. You can also use the delegate methods to perform related tasks, such as updating other parts of your interface based on the information typed by the user.

For more information about using the text field’s delegate to manage editing interactions, see UITextFieldDelegate Protocol Reference.

Interface Builder Attributes

Table 1 lists the attributes that you configure for text fields in Interface Builder.

Table 1Text Field attributes

Attribute

Description

Text

The initial text displayed by the text field. You can specify the text as a plain string or as an attributed string. If you specify an attributed string, Interface Builder provides different options for editing the font, color, and formatting of the string.

Color

The color of the text field’s text. To set this attribute programmatically, use the textColor property.

Font

The font of the text field’s text. To set this attribute programmatically, use the font property.

Alignment

The alignment of the text field’s text inside the editing area. This option is available only when formatting plain strings. To set this attribute programmatically, use the textAlignment property.

Placeholder

The placeholder text displayed by the text field. When the text field’s string is empty, the text field displays this string instead, formatting the string so as to indicate that it is not the actual text. Typing any text into the text field hides this string. To set this attribute programmatically, use the placeholder property.

Background

The background image to display when the text field is enabled. This image is displayed behind the rest of the text field’s content. To set this attribute programmatically, use the background property.

Disabled

The background image to display when the text field is disabled. This image is displayed behind the rest of the text field’s content. To set this attribute programmatically, use the disabledBackground property.

Border Style

The visual style for the text field’s border. This attribute defines the visual border, if any, drawn around the editable text region. To set this attribute programatically, use the borderStyle property.

Clear Button

The behavior of the clear button. The clear button is a built-in overlay view that the user taps to delete all of the text in a text field. Use this attribute to define when the clear button appears. To set this attribute programatically, use theclearButtonMode property.

Min Font Size

The minimum font size for the text field’s text. When the Adjust to Fit option is enabled, the text field automatically varies the font size to ensure maximum legibility of the text. You can use this attribute to specify the smallest font size that your consider appropriate for your text. To set this attribute programatically, use theminimumFontSize property.

Table 2 lists the keyboard-related attributes that you configure for text fields. This attributes correspond to properties of the UITextInputTraits protocol that the UITextField class adopts.

Table 2Keyboard attributes

Attribute

Description

Capitalization

The automatic capitalization style to apply to typed text. This attribute determines at what time the Shift key is automatically pressed. You can access the value of this attribute programmatically using the text field’s autocapitalizationType property.

Correction

The autocorrection behavior of the text field. This attribute determines whether autocorrection is enabled or disabled during typing. You can access the value of this attribute programmatically using the text field’s autocorrectionType property.

Spell Checking

The spell checking behavior of the text field. This attribute determines whether spell checking is enabled or disabled during typing. You can access the value of this attribute programmatically using the text field’s spellCheckingType property.

Keyboard Type

The style of the text field’s keyboard. This property specifies the type of keyboard displayed when the text field is active. You can access the value of this attribute programmatically using the text field’s keyboardType property.

Appearance

The visual style applied to the text field’s keyboard. Use this property to specify a dark or light keyboard. You can access the value of this attribute programmatically using the text field’s keyboardAppearance property.

Return Key

The type of return key to display on the keyboard. Use this property to configure the text and visual style applied to the keyboard’s return key. You can access the value of this attribute programmatically using the text field’s returnKeyType property.

The return key is disabled by default and becomes enabled only when the user types some text into the text field. To respond to taps in the Return key, implement the textFieldShouldReturn: method in the delegate you assign to the text field.

For information about additional attributes you can configure for a text view, see UIControl Class Reference.

Internationalization

The default language of the device affects the keyboard that pops up with the text field (including the return key). You do not need to do anything to enable this functionality; it is enabled by default. However, your text field should be able to handle input that comes from any language.

When using storyboards to build your interface, use Xcode’s base internationalization feature to configure the localizations your project supports. When you add a localization, Xcode creates a strings file for that localization. When configuring your interface programmatically, use the system’s built-in support for loading localized strings and resources. For more information about internationalizing your interface, see Internationalization and Localization Guide.

Accessibility

Text fields are accessible by default. The default accessibility traits for a button are User Interaction Enabled and Adjustable.

For more information about making iOS controls accessible, see the accessibility information in UIControl Class Reference. For general information about making your interface accessible, see Accessibility Programming Guide for iOS.

State Preservation

When you assign a value to a text field’s restorationIdentifier property, it preserves the selected range of text, if any. During the next launch cycle, the text field attempts to restore that selection. If the selection range cannot be applied to the current text, no selection is made. For more information about how state preservation and restoration works, see App Programming Guide for iOS.

  • text text Property

    The text displayed by the text field.

    Declaration

    Swift

    var text: String?

    Objective-C

    @property(nonatomic, copy) NSString *text

    Discussion

    This string is @"" by default.

    In iOS 6 and later, assigning a new value to this property also replaces the value of the attributedText property with the same text, albeit without any inherent style attributes. Instead the text view styles the new string using the font, textColor, and other style-related properties of the class.

    Availability

    Available in iOS 2.0 and later.

  • The styled text displayed by the text field.

    Declaration

    Swift

    @NSCopying var attributedText: NSAttributedString?

    Objective-C

    @property(nonatomic, copy) NSAttributedString *attributedText

    Discussion

    This property is nil by default. Assigning a new value to this property also replaces the value of the text property with the same string data, albeit without any formatting information. In addition, assigning a new a value updates the values in the font, textColor, and other style-related properties so that they reflect the style information starting at location 0 in the attributed string.

    Availability

    Available in iOS 6.0 and later.

  • The string that is displayed when there is no other text in the text field.

    Declaration

    Swift

    var placeholder: String?

    Objective-C

    @property(nonatomic, copy) NSString *placeholder

    Discussion

    This value is nil by default. The placeholder string is drawn using a 70% gray color.

    Availability

    Available in iOS 2.0 and later.

  • The styled string that is displayed when there is no other text in the text field.

    Declaration

    Swift

    @NSCopying var attributedPlaceholder: NSAttributedString?

    Objective-C

    @property(nonatomic, copy) NSAttributedString *attributedPlaceholder

    Discussion

    This property is nil by default. If set, the placeholder string is drawn using a 70% gray color and the remaining style information (except the text color) of the attributed string. Assigning a new value to this property also replaces the value of the placeholder property with the same string data, albeit without any formatting information. Assigning a new value to this property does not affect any other style-related properties of the text field.

    Availability

    Available in iOS 6.0 and later.

  • The default attributes to apply to the text.

    Declaration

    Swift

    var defaultTextAttributes: [String : AnyObject]

    Objective-C

    @property(nonatomic, copy) NSDictionary <NSString *,id> *defaultTextAttributes

    Discussion

    By default, this property returns a dictionary of text attributes with default values.

    Setting this property applies the specified attributes to the entire text of the text field. Unset attributes maintain their default values.

    Getting this property returns the previously set attributes, which may have been modified by setting properties such as font and textColor.

    Availability

    Available in iOS 7.0 and later.

  • font font Property

    The font of the text.

    Declaration

    Swift

    var font: UIFont?

    Objective-C

    @property(nonatomic, strong) UIFont *font

    Discussion

    This property applies to the entire text of the text field. It also applies to the placeholder text. The default font is a 12-point Helvetica plain font.

    If you are using styled text in iOS 6 or later, assigning a new value to this property causes the font to be applied to the entire string in the attributedText and attributedPlaceholder properties. If you want to apply the font to only a portion of the text, create a new attributed string with the desired style information and associate it with the text field.

    Availability

    Available in iOS 2.0 and later.

  • The color of the text.

    Declaration

    Swift

    var textColor: UIColor?

    Objective-C

    @property(nonatomic, strong) UIColor *textColor

    Discussion

    This property applies to the entire text string. The default value for this property is a black color. The value for the property can only be set to a non-nil value; setting this property to nil raises an exception.

    If you are using styled text in iOS 6 or later, assigning a new value to this property causes the text color to be applied to the entire string in the attributedText property. If you want to apply the color to only a portion of the text, create a new attributed string with the desired style information and associate it with the text field.

    Availability

    Available in iOS 2.0 and later.

  • The technique to use for aligning the text.

    Declaration

    Swift

    var textAlignment: NSTextAlignment

    Objective-C

    @property(nonatomic) NSTextAlignment textAlignment

    Discussion

    This property applies to the both the main text string and the placeholder string. The default value of this property is NSLeftTextAlignment.

    If you are using styled text in iOS 6 or later, assigning a new value to this property causes the text alignment to be applied to the entire string in the attributedText property. If you want to apply the alignment to only a portion of the text, create a new attributed string with the desired style information and associate it with the text field.

    Availability

    Available in iOS 2.0 and later.

  • The attributes to apply to new text being entered by the user.

    Declaration

    Swift

    var typingAttributes: [String : AnyObject]?

    Objective-C

    @property(nonatomic, copy) NSDictionary <NSString *,id> *typingAttributes

    Discussion

    This dictionary contains the attribute keys (and corresponding values) to apply to newly typed text. When the text field’s selection changes, the contents of the dictionary are cleared automatically.

    If the text field is not in editing mode, this property contains the value nil. Similarly, you cannot assign a value to this property unless the text field is currently in editing mode.

    Availability

    Available in iOS 6.0 and later.

    See Also

    editing

  • A Boolean value indicating whether the font size should be reduced in order to fit the text string into the text field’s bounding rectangle.

    Declaration

    Swift

    var adjustsFontSizeToFitWidth: Bool

    Objective-C

    @property(nonatomic) BOOL adjustsFontSizeToFitWidth

    Discussion

    Normally, the text field’s content is drawn with the font you specify in the font property. If this property is set to YEStrue, however, and the contents in the text property exceed the text field’s bounding rectangle, the receiver starts reducing the font size until the string fits or the minimum font size is reached. The text is shrunk along the baseline.

    The default value for this property is NOfalse. If you change it to YEStrue, you should also set an appropriate minimum font size by modifying the minimumFontSize property.

    Availability

    Available in iOS 2.0 and later.

    See Also

    minimumFontSize

  • The size of the smallest permissible font with which to draw the text field’s text.

    Declaration

    Swift

    var minimumFontSize: CGFloat

    Objective-C

    @property(nonatomic) CGFloat minimumFontSize

    Discussion

    When drawing text that might not fit within the bounding rectangle of the text field, you can use this property to prevent the receiver from reducing the font size to the point where it is no longer legible.

    The default value for this property is 0.0. If you enable font adjustment for the text field, you should always increase this value.

    If you are using styled text in iOS 6 or later, assigning a new value to this property causes the minimum font size to be applied to the entirety of the string in the attributedText property. If you want to apply different minimum font sizes to different parts of your string, create a new attributed string with the desired style information and associate it with the text field.

    Availability

    Available in iOS 2.0 and later.

  • A Boolean value indicating whether the text field is currently in edit mode. (read-only)

    Declaration

    Swift

    var editing: Bool { get }

    Objective-C

    @property(nonatomic, readonly, getter=isEditing) BOOL editing

    Discussion

    This property is set to YEStrue when the user begins editing text in this text field, and it is set to NOfalse again when editing ends. The text field notifies its delegate when editing begins and ends.

    Availability

    Available in iOS 2.0 and later.

  • A Boolean value indicating whether the text field removes old text when editing begins.

    Declaration

    Swift

    var clearsOnBeginEditing: Bool

    Objective-C

    @property(nonatomic) BOOL clearsOnBeginEditing

    Discussion

    If this property is set to YEStrue, the text field’s previous text is cleared when the user selects the text field to begin editing. If NOfalse, the text field places an insertion point at the place where the user tapped the field.

    Availability

    Available in iOS 2.0 and later.

  • A Boolean value indicating whether inserting text replaces the previous contents.

    Declaration

    Swift

    var clearsOnInsertion: Bool

    Objective-C

    @property(nonatomic) BOOL clearsOnInsertion

    Discussion

    The default value of this property is NOfalse. When the value of this property is YEStrue and the text field is in editing mode, the selection UI is hidden and inserting new text clears the contents of the text field and sets the value of this property back to NOfalse.

    Availability

    Available in iOS 6.0 and later.

  • A Boolean value indicating whether the user may edit the attributes of the text in the text field.

    Declaration

    Swift

    var allowsEditingTextAttributes: Bool

    Objective-C

    @property(nonatomic) BOOL allowsEditingTextAttributes

    Discussion

    If this property is set to YEStrue, the user may edit the style information of the text. In addition, pasting styled text into the text field retains any embedded style information. If NOfalse, the text field prohibits the editing of style information and strips style information from any pasted text. However, you can still set the style information programmatically using the methods of this class.

    The default value of this property is NOfalse.

    Availability

    Available in iOS 6.0 and later.

  • The border style used by the text field.

    Declaration

    Swift

    var borderStyle: UITextBorderStyle

    Objective-C

    @property(nonatomic) UITextBorderStyle borderStyle

    Discussion

    The default value for this property is UITextBorderStyleNone. If the value is set to the UITextBorderStyleRoundedRect style, the custom background image associated with the text field is ignored.

    Availability

    Available in iOS 2.0 and later.

  • The image that represents the background appearance of the text field when it is enabled.

    Declaration

    Swift

    var background: UIImage?

    Objective-C

    @property(nonatomic, strong) UIImage *background

    Discussion

    When set, the image referred to by this property replaces the standard appearance controlled by the borderStyle property. Background images are drawn in the border rectangle portion of the text field. Images you use for the text field’s background should be able to stretch to fit.

    This property is set to nil by default.

    Availability

    Available in iOS 2.0 and later.

  • The image that represents the background appearance of the text field when it is disabled.

    Declaration

    Swift

    var disabledBackground: UIImage?

    Objective-C

    @property(nonatomic, strong) UIImage *disabledBackground

    Discussion

    Background images are drawn in the border rectangle portion of the text field. Images you use for the text field’s background should be able to stretch to fit. This property is ignored if the background property is not also set.

    This property is set to nil by default.

    Availability

    Available in iOS 2.0 and later.

    See Also

    background

  • Controls when the standard clear button appears in the text field.

    Declaration

    Swift

    var clearButtonMode: UITextFieldViewMode

    Objective-C

    @property(nonatomic) UITextFieldViewMode clearButtonMode

    Discussion

    The standard clear button is displayed at the right side of the text field, when the text field has contents, as a way for the user to remove text quickly. This button appears automatically based on the value set for this property.

    The default value for this property is UITextFieldViewModeNever.

    Availability

    Available in iOS 2.0 and later.

  • The overlay view displayed on the left (or leading) side of the text field.

    Declaration

    Swift

    var leftView: UIView?

    Objective-C

    @property(nonatomic, strong) UIView *leftView

    Discussion

    You can use the left overlay view to indicate the intended behavior of the text field. For example, you might display a magnifying glass in this location to indicate that the text field is a search field. The left overlay view flips automatically in a right-to-left user interface.

    The left overlay view is placed in the rectangle returned by the leftViewRectForBounds: method of the receiver. The image associated with this property should fit the given rectangle. If it does not fit, it is scaled to fit. If you specify a control for your view, the control tracks and sends actions as usual.

    Availability

    Available in iOS 2.0 and later.

  • Controls when the left overlay view appears in the text field.

    Declaration

    Swift

    var leftViewMode: UITextFieldViewMode

    Objective-C

    @property(nonatomic) UITextFieldViewMode leftViewMode

    Discussion

    The default value for this property is UITextFieldViewModeNever. Note that the left overlay view flips automatically in a right-to-left user interface.

    Availability

    Available in iOS 2.0 and later.

  • The overlay view displayed on the right (or trailing) side of the text field.

    Declaration

    Swift

    var rightView: UIView?

    Objective-C

    @property(nonatomic, strong) UIView *rightView

    Discussion

    You can use the right overlay view to provide indicate additional features available for the text field. For example, you might display a bookmarks button in this location to allow the user to select from a set of predefined items. The right overlay view flips automatically in a right-to-left user interface.

    The right overlay view is placed in the rectangle returned by the rightViewRectForBounds: method of the receiver. The image associated with this property should fit the given rectangle. If it does not fit, it is scaled to fit. If you specify a control for your view, that control tracks and sends actions as usual.

    If your right overlay view overlaps a sibling view, such as the clear button, you must use the UITextFieldViewMode to implement proper behavior. For example, if clearButtonMode is set to display the clear button, you can set the right overlay view’s rightViewMode to UITextFieldViewModeUnlessEditing to reveal the clear button during editing when the text field has contents.

    Availability

    Available in iOS 2.0 and later.

  • Controls when the right overlay view appears in the text field.

    Declaration

    Swift

    var rightViewMode: UITextFieldViewMode

    Objective-C

    @property(nonatomic) UITextFieldViewMode rightViewMode

    Discussion

    The default value for this property is UITextFieldViewModeNever. Note that the right overlay view flips automatically in a right-to-left user interface.

    Availability

    Available in iOS 2.0 and later.

  • The receiver’s delegate.

    Declaration

    Swift

    weak var delegate: UITextFieldDelegate?

    Objective-C

    @property(nonatomic, weak) id< UITextFieldDelegate > delegate

    Discussion

    A text field delegate responds to editing-related messages from the text field. You can use the delegate to respond to the text entered by the user and to some special commands, such as when the return button is pressed.

    Availability

    Available in iOS 2.0 and later.

  • Returns the drawing rectangle for the text field’s text.

    Declaration

    Swift

    func textRectForBounds(_ bounds: CGRect) -> CGRect

    Objective-C

    - (CGRect)textRectForBounds:(CGRect)bounds

    Parameters

    bounds

    The bounding rectangle of the receiver.

    Return Value

    The computed drawing rectangle for the label’s text.

    Discussion

    You should not call this method directly. If you want to customize the drawing rectangle for the text, you can override this method and return a different rectangle.

    The default implementation of this method returns a rectangle that is derived from the control’s original bounds, but which does not include the area occupied by the receiver’s border or overlay views.

    Availability

    Available in iOS 2.0 and later.

  • Draws the receiver’s text in the specified rectangle.

    Declaration

    Swift

    func drawTextInRect(_ rect: CGRect)

    Objective-C

    - (void)drawTextInRect:(CGRect)rect

    Parameters

    rect

    The rectangle in which to draw the text.

    Discussion

    You should not call this method directly. If you want to customize the drawing behavior for the text, you can override this method to do your drawing.

    By the time this method is called, the current graphics context is already configured with the default environment and text color for drawing. In your overridden method, you can configure the current context further and then invoke super to do the actual drawing or you can do the drawing yourself. If you do render the text yourself, you should not invoke super.

    Availability

    Available in iOS 2.0 and later.

  • Returns the drawing rectangle for the text field’s placeholder text

    Declaration

    Swift

    func placeholderRectForBounds(_ bounds: CGRect) -> CGRect

    Objective-C

    - (CGRect)placeholderRectForBounds:(CGRect)bounds

    Parameters

    bounds

    The bounding rectangle of the receiver.

    Return Value

    The computed drawing rectangle for the placeholder text.

    Discussion

    You should not call this method directly. If you want to customize the drawing rectangle for the placeholder text, you can override this method and return a different rectangle.

    If the placeholder string is empty or nil, this method is not called.

    Availability

    Available in iOS 2.0 and later.

  • Draws the receiver’s placeholder text in the specified rectangle.

    Declaration

    Swift

    func drawPlaceholderInRect(_ rect: CGRect)

    Objective-C

    - (void)drawPlaceholderInRect:(CGRect)rect

    Parameters

    rect

    The rectangle in which to draw the placeholder text.

    Discussion

    You should not call this method directly. If you want to customize the drawing behavior for the placeholder text, you can override this method to do your drawing.

    By the time this method is called, the current graphics context is already configured with the default environment and text color for drawing. In your overridden method, you can configure the current context further and then invoke super to do the actual drawing or do the drawing yourself. If you do render the text yourself, you should not invoke super.

    Availability

    Available in iOS 2.0 and later.

  • Returns the receiver’s border rectangle.

    Declaration

    Swift

    func borderRectForBounds(_ bounds: CGRect) -> CGRect

    Objective-C

    - (CGRect)borderRectForBounds:(CGRect)bounds

    Parameters

    bounds

    The bounding rectangle of the receiver.

    Return Value

    The border rectangle for the receiver.

    Discussion

    You should not call this method directly. If you want to provide a different border rectangle for drawing, you can override this method and return that rectangle.

    The default implementation of this method returns the original bounds rectangle.

    Availability

    Available in iOS 2.0 and later.

  • Returns the rectangle in which editable text can be displayed.

    Declaration

    Swift

    func editingRectForBounds(_ bounds: CGRect) -> CGRect

    Objective-C

    - (CGRect)editingRectForBounds:(CGRect)bounds

    Parameters

    bounds

    The bounding rectangle of the receiver.

    Return Value

    The computed editing rectangle for the text.

    Discussion

    You should not call this method directly. If you want to provide a different editing rectangle for the text, you can override this method and return that rectangle. By default, this method returns a region in the text field that is not occupied by any overlay views.

    Availability

    Available in iOS 2.0 and later.

  • Returns the drawing rectangle for the built-in clear button.

    Declaration

    Swift

    func clearButtonRectForBounds(_ bounds: CGRect) -> CGRect

    Objective-C

    - (CGRect)clearButtonRectForBounds:(CGRect)bounds

    Parameters

    bounds

    The bounding rectangle of the receiver.

    Return Value

    The rectangle in which to draw the clear button.

    Discussion

    You should not call this method directly. If you want to place the clear button in a different location, you can override this method and return the new rectangle. Your method should call the super implementation and modify the returned rectangle’s origin only. Changing the size of the clear button may cause unnecessary distortion of the button image.

    Availability

    Available in iOS 2.0 and later.

  • Returns the drawing rectangle of the receiver’s left overlay view.

    Declaration

    Swift

    func leftViewRectForBounds(_ bounds: CGRect) -> CGRect

    Objective-C

    - (CGRect)leftViewRectForBounds:(CGRect)bounds

    Parameters

    bounds

    The bounding rectangle of the receiver.

    Return Value

    The rectangle in which to draw the left overlay view.

    Discussion

    You should not call this method directly. If you want to place the left overlay view in a different location, you can override this method and return the new rectangle. Note that the drawing rectangle remains unchanged in a right-to-left user interface.

    Availability

    Available in iOS 2.0 and later.

  • Returns the drawing location of the receiver’s right overlay view.

    Declaration

    Swift

    func rightViewRectForBounds(_ bounds: CGRect) -> CGRect

    Objective-C

    - (CGRect)rightViewRectForBounds:(CGRect)bounds

    Parameters

    bounds

    The bounding rectangle of the receiver.

    Return Value

    The rectangle in which to draw the right overlay view.

    Discussion

    You should not call this method directly. If you want to place the right overlay view in a different location, you can override this method and return the new rectangle. Note that the drawing rectangle remains unchanged in a right-to-left user interface.

    Availability

    Available in iOS 2.0 and later.

  • The custom input view to display when the text field becomes the first responder.

    Declaration

    Swift

    var inputView: UIView?

    Objective-C

    @property(readwrite, strong) UIView *inputView

    Discussion

    If the value in this property is nil, the text field displays the standard system keyboard when it becomes first responder. Assigning a custom view to this property causes that view to be presented instead.

    The default value of this property is nil.

    Availability

    Available in iOS 3.2 and later.

  • The custom accessory view to display when the text field becomes the first responder

    Declaration

    Swift

    var inputAccessoryView: UIView?

    Objective-C

    @property(readwrite, strong) UIView *inputAccessoryView

    Discussion

    The default value of this property is nil. Assigning a view to this property causes that view to be displayed above the standard system keyboard (or above the custom input view if one is provided) when the text field becomes the first responder. For example, you could use this property to attach a custom toolbar to the keyboard.

    Availability

    Available in iOS 3.2 and later.

  • The type of border drawn around the text field.

    Declaration

    Swift

    enum UITextBorderStyle : Int { case None case Line case Bezel case RoundedRect }

    Objective-C

    typedef enum UITextBorderStyle : NSInteger { UITextBorderStyleNone, UITextBorderStyleLine, UITextBorderStyleBezel, UITextBorderStyleRoundedRect } UITextBorderStyle;

    Constants

    • none

      UITextBorderStyleNone

      The text field does not display a border.

      Available in iOS 2.0 and later.

    • line

      UITextBorderStyleLine

      Displays a thin rectangle around the text field.

      Available in iOS 2.0 and later.

    • bezel

      UITextBorderStyleBezel

      Displays a bezel-style border for the text field. This style is typically used for standard data-entry fields.

      Available in iOS 2.0 and later.

    • roundedRect

      UITextBorderStyleRoundedRect

      Displays a rounded-style border for the text field.

      Available in iOS 2.0 and later.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • Defines the times at which overlay views appear in a text field.

    Declaration

    Swift

    enum UITextFieldViewMode : Int { case Never case WhileEditing case UnlessEditing case Always }

    Objective-C

    typedef enum UITextFieldViewMode : NSInteger { UITextFieldViewModeNever, UITextFieldViewModeWhileEditing, UITextFieldViewModeUnlessEditing, UITextFieldViewModeAlways } UITextFieldViewMode;

    Constants

    • never

      UITextFieldViewModeNever

      The overlay view never appears.

      Available in iOS 2.0 and later.

    • whileEditing

      UITextFieldViewModeWhileEditing

      The overlay view is displayed only while text is being edited in the text field.

      Available in iOS 2.0 and later.

    • unlessEditing

      UITextFieldViewModeUnlessEditing

      The overlay view is displayed only when text is not being edited.

      Available in iOS 2.0 and later.

    • always

      UITextFieldViewModeAlways

      The overlay view is always displayed if the text field contains text.

      Available in iOS 2.0 and later.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.