---
title: UITextInteraction
framework: uikit
role: symbol
role_heading: Class
path: uikit/uitextinteraction
---

# UITextInteraction

An interaction that provides text selection gestures and UI to custom text views.

## Declaration

```swift
@MainActor class UITextInteraction
```

## Mentioned in

Adopting system selection UI in custom text views

## Overview

Overview Use UITextInteraction to provide your custom text views the same text selection gestures and UI available in native text views like UITextView and UITextField. When creating a text interaction, choose a mode that matches the state of the text, UITextInteractionMode.editable or UITextInteractionMode.nonEditable. Then set the textInput property to an object that conforms to UITextInput, and add the interaction to a view. // Create a selection interaction for non-editable content. let selectionInteraction = UITextInteraction(for: .nonEditable)

// Assign `textInput` to your view that implements the `UITextInput` protocol // to get more control over the selection behavior and the text input system. selectionInteraction.textInput = customTextView

// Add the interaction to the view. customTextView.addInteraction(selectionInteraction) If your custom text view supports editable and non-editable text, create two interactions — one for each mode — and add the interaction that matches the state of text to the view while removing the other interaction. override func becomeFirstResponder() -> Bool {     let isFirstResponder = self.isFirstResponder     let result = super.becomeFirstResponder()          if isFirstResponder == false && self.isFirstResponder == true {         customTextView.removeInteraction(nonEditableTextInteraction)         customTextView.addInteraction(editableTextInteraction)     }          return result }

override func resignFirstResponder() -> Bool {     let isFirstResponder = self.isFirstResponder     let result = super.resignFirstResponder()          if isFirstResponder == true && self.isFirstResponder == false {         customTextView.removeInteraction(editableTextInteraction)         customTextView.addInteraction(nonEditableTextInteraction)     }          return result } If your app provides other gestures in the same view hierarchy, you can use the require(toFail:) method to set up failure requirements between your app’s gestures and the text interaction gestures listed in the gesturesForFailureRequirements property.

## Topics

### Creating text interactions

- [init(for:)](uikit/uitextinteraction/init(for:).md)

### Handling text input and interaction events

- [textInput](uikit/uitextinteraction/textinput.md)
- [delegate](uikit/uitextinteraction/delegate.md)
- [UITextInteractionDelegate](uikit/uitextinteractiondelegate.md)

### Getting interaction information

- [gesturesForFailureRequirements](uikit/uitextinteraction/gesturesforfailurerequirements.md)
- [textInteractionMode](uikit/uitextinteraction/textinteractionmode.md)
- [UITextInteractionMode](uikit/uitextinteractionmode.md)

### Initializers

- [init(forMode:)](uikit/uitextinteraction/init(formode:).md)

## Relationships

### Inherits From

- [NSObject](objectivec/nsobject-swift.class.md)

### Conforms To

- [CVarArg](swift/cvararg.md)
- [CustomDebugStringConvertible](swift/customdebugstringconvertible.md)
- [CustomStringConvertible](swift/customstringconvertible.md)
- [Equatable](swift/equatable.md)
- [Hashable](swift/hashable.md)
- [NSObjectProtocol](objectivec/nsobjectprotocol.md)
- [Sendable](swift/sendable.md)
- [UIInteraction](uikit/uiinteraction.md)

## See Also

### Text interactions

- [UITextInteractionDelegate](uikit/uitextinteractiondelegate.md)
- [UITextInteractionMode](uikit/uitextinteractionmode.md)
