---
title: AttributedTextSelection
framework: swiftui
role: symbol
role_heading: Structure
path: swiftui/attributedtextselection
---

# AttributedTextSelection

Represents a selection of attributed text.

## Declaration

```swift
struct AttributedTextSelection
```

## Overview

Overview A selection is either an insertion point (e.g. a cursor in the text), or spans over a range of characters. While that range is always visually contiguous, it may not be logically contiguous in the text storage. Specifically, a single selection value cannot represent multiple cursors. This is frequently used to represent selection of text in a 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: AttributedString = ""     @State var selection = AttributedTextSelection()

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(in: text))         }     }

private func getSubstrings(         text: String, indices: AttributedTextSelection.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: AttributedString = ""     @State var selection = AttributedTextSelection()

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(in: text))         }         .textSelectionAffinity(.upstream)     }

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

struct SuggestionsView: View { ... } note: TextSelectionAffinity, TextEditor

## Topics

### Structures

- [AttributedTextSelection.Attributes](swiftui/attributedtextselection/attributes.md)

### Initializers

- [init()](swiftui/attributedtextselection/init().md)
- [init(insertionPoint:typingAttributes:)](swiftui/attributedtextselection/init(insertionpoint:typingattributes:).md)
- [init(range:)](swiftui/attributedtextselection/init(range:).md)
- [init(ranges:)](swiftui/attributedtextselection/init(ranges:).md)

### Instance Methods

- [affinity(in:)](swiftui/attributedtextselection/affinity(in:).md)
- [attributes(in:)](swiftui/attributedtextselection/attributes(in:).md)
- [indices(in:)](swiftui/attributedtextselection/indices(in:).md)
- [typingAttributes(in:)](swiftui/attributedtextselection/typingattributes(in:).md)

### Enumerations

- [AttributedTextSelection.Indices](swiftui/attributedtextselection/indices.md)

## Relationships

### Conforms To

- [Equatable](swift/equatable.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)
- [TextSelectionAffinity](swiftui/textselectionaffinity.md)
