---
title: TextSelection
framework: swiftui
role: symbol
role_heading: Structure
path: swiftui/textselection
---

# TextSelection

Represents a selection of text.

## Declaration

```swift
struct TextSelection
```

## Overview

Overview A selection is either an insertion point (e.g. a cursor in the text), a selection over a range of text or on macOS, multiple selections. This is frequently used to represent selection of text in a TextField or TextEditor. The following example shows a text editor that leverages text selection to offer live suggestions based on the current selection. 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))         }     }

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

struct SuggestionsView: View { ... } You can also use the textSelectionAffinity(_:) modifier to specify a selection affinity on the given hierarchy: 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

### Initializers

- [init(insertionPoint:)](swiftui/textselection/init(insertionpoint:).md)
- [init(range:)](swiftui/textselection/init(range:).md)
- [init(ranges:)](swiftui/textselection/init(ranges:).md)

### Instance Properties

- [affinity](swiftui/textselection/affinity.md)
- [indices](swiftui/textselection/indices-swift.property.md)
- [isInsertion](swiftui/textselection/isinsertion.md)

### Enumerations

- [TextSelection.Indices](swiftui/textselection/indices-swift.enum.md)

## Relationships

### Conforms To

- [Equatable](swift/equatable.md)
- [Hashable](swift/hashable.md)

## See Also

### Selecting text

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