One Logo Pool Ball
SwiftUI · IOS API REFERENCE

TextSelection

struct · iOS 18.0+ · SDK 27.1

struct TextSelection

Represents a selection of text. 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 ``View/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 { ... }

Members (7)

get · iOS 18.0+

isInsertion

TypeScript
get isInsertion(): boolean
Swift declaration
var isInsertion: Bool { get }

Return `true` if the selection is an insertion point. An insertion point effectively represents a range that contains no characters, indicating a location in the string. This location refers to the point in the text where new characters will be inserted. In other words, it represents cases when the start and end index are equal.

init · iOS 18.0+

init

TypeScript
static initInsertionPoint(insertionPoint: SDKHandle): TextSelection
Swift declaration
init(insertionPoint: String.Index)

Create a selection at a given insertion point. - Parameters: - insertionPoint: A `String.Index` representing the insertion point.

init · iOS 18.0+

init

TypeScript
static initRange(range: SDKHandle): TextSelection
Swift declaration
init(range: Range<String.Index>)

Create a single selection with a given range. - Parameters: - range: A range of `String.Index` representing the selection.

init · iOS 18.0+

init

TypeScript
static initRanges(ranges: SDKHandle): TextSelection
Swift declaration
init(ranges: RangeSet<String.Index>)

Create multiple selections with a given range-set. - Parameters: - ranges: A range-set of `String.Index` representing the selection.

get · iOS 18.0+

indices

TypeScript
get indices(): TextSelection_Indices
Swift declaration
var indices: TextSelection.Indices

Return the current text selection indices.

set · iOS 18.0+

indices

TypeScript
set indices(value: TextSelection_Indices)
Swift declaration
var indices: TextSelection.Indices

Return the current text selection indices.

get · iOS 18.0+

affinity

TypeScript
get affinity(): TextSelectionAffinity
Swift declaration
var affinity: TextSelectionAffinity { get }

Return the selection affinity of the selection.

Skipped declarations

No declarations were skipped in this snapshot.

All SwiftUI types →