---
title: UITextField
framework: uikit
role: symbol
role_heading: Class
path: uikit/uitextfield
---

# UITextField

An object that displays an editable text area in your interface.

## Declaration

```swift
@MainActor class UITextField
```

## Mentioned in

About app development with UIKit Adding a custom font to your app Adding user-focusable elements to a tvOS app Adding Writing Tools support to a custom UIKit view Adopting system selection UI in custom text views Customizing Writing Tools behavior for UIKit views

## Overview

Overview 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.

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. Show and hide 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. note: The appearance of the keyboard has the potential to obscure portions of your user interface. You should update your interface as needed to ensure that the text field being edited is visible. Use the keyboard notifications to detect the appearance and disappearance of the keyboard and to make necessary changes to your interface. For more information, see Respond to keyboard-related notifications. 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 doesn’t 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 Validate text and manage the editing process. Configure 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 Respond 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: keyboardWillShowNotification keyboardDidShowNotification keyboardWillHideNotification keyboardDidHideNotification keyboardWillChangeFrameNotification keyboardDidChangeFrameNotification 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 the following image. In other cases, you can resize your main content view so that it isn’t covered by the keyboard.

Format 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 Formatter 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 doesn’t provide built-in support for formatting its string using an Formatter 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(_:shouldChangeCharactersIn:replacementString:) method to validate and format text while the user is typing. For information about how to use formatter objects, see Data Formatting. Use 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. The following code 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. 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. Validate text and manage 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’s 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. Interface Builder attributes The following table lists the attributes that you configure for text fields in Interface Builder.  |   |   |   |   |   |   |   |   |   |   |  The following table 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.  |   |   |   |   |   |   |  For information about additional attributes you can configure for a text view, see UIControl. Internationalization The default language of the device affects the keyboard that pops up with the text field (including the return key). You don’t need to do anything to enable this functionality; it’s 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 Localization. Accessibility Text fields are accessible by default. The default accessibility trait for a text field is User Interaction Enabled. For more information about making iOS controls accessible, see the accessibility information in UIControl. For general information about making your interface accessible, see Accessibility for UIKit. 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 can’t be applied to the current text, no selection is made. For design guidance, see Human Interface Guidelines.

## Topics

### Validating and handling edits

- [delegate](uikit/uitextfield/delegate.md)
- [UITextFieldDelegate](uikit/uitextfielddelegate.md)

### Configuring the text attributes

- [text](uikit/uitextfield/text.md)
- [attributedText](uikit/uitextfield/attributedtext.md)
- [placeholder](uikit/uitextfield/placeholder.md)
- [attributedPlaceholder](uikit/uitextfield/attributedplaceholder.md)
- [defaultTextAttributes](uikit/uitextfield/defaulttextattributes.md)
- [font](uikit/uitextfield/font.md)
- [textColor](uikit/uitextfield/textcolor.md)
- [textAlignment](uikit/uitextfield/textalignment.md)
- [typingAttributes](uikit/uitextfield/typingattributes.md)
- [UITextField.BorderStyle](uikit/uitextfield/borderstyle-swift.enum.md)

### Sizing the text field’s text

- [adjustsFontSizeToFitWidth](uikit/uitextfield/adjustsfontsizetofitwidth.md)
- [minimumFontSize](uikit/uitextfield/minimumfontsize.md)
- [sizingRule](uikit/uiletterformawareadjusting/sizingrule.md)

### Managing the editing behavior

- [isEditing](uikit/uitextfield/isediting.md)
- [clearsOnBeginEditing](uikit/uitextfield/clearsonbeginediting.md)
- [clearsOnInsertion](uikit/uitextfield/clearsoninsertion.md)
- [allowsEditingTextAttributes](uikit/uitextfield/allowseditingtextattributes.md)
- [UITextField.DidEndEditingReason](uikit/uitextfield/didendeditingreason.md)
- [didEndEditingReasonUserInfoKey](uikit/uitextfield/didendeditingreasonuserinfokey.md)
- [textDidBeginEditingNotification](uikit/uitextfield/textdidbegineditingnotification.md)
- [textDidChangeNotification](uikit/uitextfield/textdidchangenotification.md)
- [textDidEndEditingNotification](uikit/uitextfield/textdidendeditingnotification.md)

### Setting the view’s background appearance

- [borderStyle](uikit/uitextfield/borderstyle-swift.property.md)
- [background](uikit/uitextfield/background.md)
- [disabledBackground](uikit/uitextfield/disabledbackground.md)

### Managing overlay views

- [clearButtonMode](uikit/uitextfield/clearbuttonmode.md)
- [leftView](uikit/uitextfield/leftview.md)
- [leftViewMode](uikit/uitextfield/leftviewmode.md)
- [rightView](uikit/uitextfield/rightview.md)
- [rightViewMode](uikit/uitextfield/rightviewmode.md)
- [UITextField.ViewMode](uikit/uitextfield/viewmode.md)

### Drawing and positioning overrides

- [textRect(forBounds:)](uikit/uitextfield/textrect(forbounds:).md)
- [drawText(in:)](uikit/uitextfield/drawtext(in:).md)
- [placeholderRect(forBounds:)](uikit/uitextfield/placeholderrect(forbounds:).md)
- [drawPlaceholder(in:)](uikit/uitextfield/drawplaceholder(in:).md)
- [borderRect(forBounds:)](uikit/uitextfield/borderrect(forbounds:).md)
- [editingRect(forBounds:)](uikit/uitextfield/editingrect(forbounds:).md)
- [clearButtonRect(forBounds:)](uikit/uitextfield/clearbuttonrect(forbounds:).md)
- [leftViewRect(forBounds:)](uikit/uitextfield/leftviewrect(forbounds:).md)
- [rightViewRect(forBounds:)](uikit/uitextfield/rightviewrect(forbounds:).md)

### Replacing the system input views

- [inputView](uikit/uitextfield/inputview.md)
- [inputAccessoryView](uikit/uitextfield/inputaccessoryview.md)

### Supporting state restoration

- [interactionState](uikit/uitextfield/interactionstate.md)

### Structures

- [UITextField.TextDidBeginEditingMessage](uikit/uitextfield/textdidbegineditingmessage.md)
- [UITextField.TextDidChangeMessage](uikit/uitextfield/textdidchangemessage.md)
- [UITextField.TextDidEndEditingMessage](uikit/uitextfield/textdidendeditingmessage.md)

## Relationships

### Inherits From

- [UIControl](uikit/uicontrol.md)

### Inherited By

- [UISearchTextField](uikit/uisearchtextfield.md)

### Conforms To

- [CALayerDelegate](quartzcore/calayerdelegate.md)
- [CLBodyIdentifiable](corelocation/clbodyidentifiable.md)
- [CMBodyIdentifiable](coremotion/cmbodyidentifiable.md)
- [CVarArg](swift/cvararg.md)
- [Copyable](swift/copyable.md)
- [CustomDebugStringConvertible](swift/customdebugstringconvertible.md)
- [CustomStringConvertible](swift/customstringconvertible.md)
- [Equatable](swift/equatable.md)
- [Escapable](swift/escapable.md)
- [Hashable](swift/hashable.md)
- [NSCoding](foundation/nscoding.md)
- [NSObjectProtocol](objectivec/nsobjectprotocol.md)
- [NSTouchBarProvider](appkit/nstouchbarprovider.md)
- [Sendable](swift/sendable.md)
- [SendableMetatype](swift/sendablemetatype.md)
- [UIAccessibilityIdentification](uikit/uiaccessibilityidentification.md)
- [UIActivityItemsConfigurationProviding](uikit/uiactivityitemsconfigurationproviding.md)
- [UIAppearance](uikit/uiappearance.md)
- [UIAppearanceContainer](uikit/uiappearancecontainer.md)
- [UIContentSizeCategoryAdjusting](uikit/uicontentsizecategoryadjusting.md)
- [UIContextMenuInteractionDelegate](uikit/uicontextmenuinteractiondelegate.md)
- [UICoordinateSpace](uikit/uicoordinatespace.md)
- [UIDynamicItem](uikit/uidynamicitem.md)
- [UIFocusEnvironment](uikit/uifocusenvironment.md)
- [UIFocusItem](uikit/uifocusitem.md)
- [UIFocusItemContainer](uikit/uifocusitemcontainer.md)
- [UIKeyInput](uikit/uikeyinput.md)
- [UILargeContentViewerItem](uikit/uilargecontentvieweritem.md)
- [UILetterformAwareAdjusting](uikit/uiletterformawareadjusting.md)
- [UIPasteConfigurationSupporting](uikit/uipasteconfigurationsupporting.md)
- [UIPopoverPresentationControllerSourceItem](uikit/uipopoverpresentationcontrollersourceitem.md)
- [UIResponderStandardEditActions](uikit/uiresponderstandardeditactions.md)
- [UITextDraggable](uikit/uitextdraggable.md)
- [UITextDroppable](uikit/uitextdroppable.md)
- [UITextInput](uikit/uitextinput.md)
- [UITextInputTraits](uikit/uitextinputtraits.md)
- [UITextPasteConfigurationSupporting](uikit/uitextpasteconfigurationsupporting.md)
- [UITraitChangeObservable](uikit/uitraitchangeobservable-67e94.md)
- [UITraitEnvironment](uikit/uitraitenvironment.md)
- [UIUserActivityRestoring](uikit/uiuseractivityrestoring.md)

## See Also

### Text views

- [UILabel](uikit/uilabel.md)
- [UITextView](uikit/uitextview.md)
- [Drag and drop customization](uikit/drag-and-drop-customization.md)
