---
title: TextSelectionAffinity
framework: swiftui
role: symbol
role_heading: Enumeration
path: swiftui/textselectionaffinity
---

# TextSelectionAffinity

A representation of the direction or association of a selection or cursor relative to a text character. This concept becomes much more prominent when dealing with bidirectional text (text that contains both LTR and RTL scripts, like English and Arabic combined).

## Declaration

```swift
enum TextSelectionAffinity
```

## Overview

Overview This type also determines whether, for example, the insertion point appears after the last character on a line or before the first character on the following line in cases where text wraps across line boundaries. Given the scenario hello|مرحبا, where | represents the cursor & مرحبا represents “hello” in Arabic, the ambiguity arises because: If the cursor is associated with the end of the English word, it would be as if you’re continuing to type in English (LTR). If the cursor is associated with the beginning of the Arabic word, it would also be as if you’re continuing to type in Arabic (RTL). TextSelectionAffinity helps resolve this ambiguity by determining the direction or association of the cursor relative to the surrounding text. You can configure the selection affinity on a given hierarchy by using the textSelectionAffinity(_:) modifier: struct SuggestionTextEditor: View {     @State var text: String = ""     @State var selection: TextSelection? = nil

var body: some View {         VStack {             TextEditor(text: $text, selection: $selection)             // A helper view that offers live suggestions based on selection.             SuggestionsView(                 substrings: getSubstrings(text: text, indices: selection?.indices))         }         .textSelectionAffinity(.upstream)     }

private func getSubstrings(         text: String, indices: TextSelection.Indices?     ) -> [Substring] {         // Resolve substrings representing the current selection...     } }

struct SuggestionsView: View { ... }

## Topics

### Enumeration Cases

- [TextSelectionAffinity.automatic](swiftui/textselectionaffinity/automatic.md)
- [TextSelectionAffinity.downstream](swiftui/textselectionaffinity/downstream.md)
- [TextSelectionAffinity.upstream](swiftui/textselectionaffinity/upstream.md)

## Relationships

### Conforms To

- [Equatable](swift/equatable.md)
- [Hashable](swift/hashable.md)
- [Sendable](swift/sendable.md)
- [SendableMetatype](swift/sendablemetatype.md)

## See Also

### Selecting text

- [textSelection(_:)](swiftui/view/textselection(_:).md)
- [TextSelectability](swiftui/textselectability.md)
- [TextSelection](swiftui/textselection.md)
- [textSelectionAffinity(_:)](swiftui/view/textselectionaffinity(_:).md)
- [textSelectionAffinity](swiftui/environmentvalues/textselectionaffinity.md)
- [AttributedTextSelection](swiftui/attributedtextselection.md)
