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
type TextSelectionAffinity = 'automatic' | 'downstream' | 'upstream';enum TextSelectionAffinityPass a literal, or build one with SwiftUI.TextSelectionAffinity from import { SwiftUI } from 'swiftui-react-native'.
Type properties
'automatic'SwiftUI.TextSelectionAffinity.automaticA selection affinity determined by the framework based on the current context.
'downstream'SwiftUI.TextSelectionAffinity.downstreamAn downstream selection affinity. In this case, the cursor is associated with the character immediately after it.
'upstream'SwiftUI.TextSelectionAffinity.upstreamAn upstream selection affinity. In this case, the cursor is associated with the character immediately before it.
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
View.textSelectionAffinity(_:) modifier:
struct SuggestionTextEditor: View {
@State private var text: String = ""
@State private 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 { ... }From Apple’s SwiftUI documentation. Code samples there are Swift.
Used by
See also
More in Text and symbols.
- DisabledTextSelectabilityA selectability type that disables text selection by the person using your app.
- EnabledTextSelectabilityA selectability type that enables text selection by the person using your app.
- FindContextThe status of the find navigator for views which support text editing.
- FixedTextVariantThe default text variant preference that chooses the largest available variant.
- FontAn environment-dependent font.
- FontChainFont with chained calls: SwiftUI.Font.bold(...).
- FontContextInformation used to resolve a font.
- FontDesignA design to use for fonts.
- FontLeadingA line spacing adjustment that you can apply to a font.
- FontWeightA weight to use for fonts.
- FontWidthA width to use for fonts that have multiple widths.
- SubmitLabelA semantic label describing the label of submission within a view hierarchy.