SwiftUI
Text Selection
iOS Development
Swift
Text Label

How do I allow text selection on a Text label in SwiftUI?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

SwiftUI supports selectable text in modern iOS versions using a dedicated modifier. The behavior depends on platform and OS availability, so fallback handling is important if your deployment target includes older versions. For custom interactions, bridging to UIKit may still be necessary.

Core Sections

Native SwiftUI text selection

In iOS 15 and newer, use textSelection.

swift
Text("Long press to select this text")
    .textSelection(.enabled)

This enables user copy selection behavior in supported contexts.

Scope selection at container level

You can enable selection for multiple text views.

swift
1VStack(alignment: .leading) {
2    Text("Line one")
3    Text("Line two")
4}
5.textSelection(.enabled)

Helpful for read-only content blocks.

Availability checks

Guard behavior if supporting older versions.

swift
1if #available(iOS 15.0, *) {
2    Text(content).textSelection(.enabled)
3} else {
4    Text(content)
5}

UIKit fallback for older systems

For legacy support with advanced copy features, embed UITextView via UIViewRepresentable.

UX considerations

Selection can conflict with tap gestures or drag gestures. Test gesture priority where text is interactive.

Validation and production readiness

Verify selection works in dark mode, dynamic type sizes, and localized long strings. Accessibility validation is important for assistive technologies.

UIKit bridge for older platforms

If deployment target includes pre-iOS-15 devices, wrap UITextView to provide selectable text behavior.

swift
1import SwiftUI
2
3struct SelectableTextView: UIViewRepresentable {
4    let text: String
5
6    func makeUIView(context: Context) -> UITextView {
7        let tv = UITextView()
8        tv.isEditable = false
9        tv.isSelectable = true
10        tv.isScrollEnabled = false
11        tv.backgroundColor = .clear
12        return tv
13    }
14
15    func updateUIView(_ uiView: UITextView, context: Context) {
16        uiView.text = text
17    }
18}

Use this only where native textSelection is unavailable, since UIKit bridging adds maintenance overhead.

Interaction tuning in complex layouts

Selection can conflict with long-press gestures, list row taps, or custom drag actions. Test in contexts like List, ScrollView, and cells with buttons. If needed, move selection to a dedicated detail screen where gesture ownership is simpler.

Accessibility and localization checks

Long localized strings can wrap differently and affect selection handles. Validate with dynamic type sizes and VoiceOver enabled. Selection features are most useful when users can reliably copy full values such as order IDs, URLs, or support codes.

Production checklist and verification loop

A reliable implementation needs more than a working snippet. Add a small verification loop that runs in CI and after dependency upgrades. Start with golden examples that represent normal input, boundary input, and one malformed input. Then validate output values, output shape or schema, and failure messages. This catches silent behavior drift early.

Document assumptions directly in the code comments near the transformation or query logic. Teams often forget whether behavior is strict, permissive, or backward-compatibility focused. Clear assumptions reduce future refactor risk.

For performance-sensitive paths, capture a baseline metric and compare after every change. The metric can be latency, memory use, or throughput depending on workload. Keep benchmark inputs realistic so results are meaningful.

Finally, expose observability signals that tell you when this logic starts failing in production. Useful signals include error counts, validation failures, and rate of fallback paths. A short checklist, a few deterministic tests, and lightweight monitoring are usually enough to keep this solution stable as surrounding systems evolve.

For support-focused apps, expose copy feedback such as a short confirmation message after selection and copy actions. Small UX details improve trust when users copy codes, links, and account identifiers.

Common Pitfalls

  • Expecting textSelection to work on unsupported iOS versions.
  • Applying selection and conflicting gestures without testing.
  • Forgetting container-level modifier impacts nested text views.
  • Assuming selectable text implies editable text behavior.
  • Skipping accessibility checks for copy workflows.

Summary

  • SwiftUI text selection is available with .textSelection(.enabled) on supported platforms.
  • Use availability checks for older deployment targets.
  • Consider UIKit bridges for legacy advanced behavior.
  • Test gesture interactions and accessibility.
  • Keep selection behavior consistent across screens.

Course illustration
Course illustration

All Rights Reserved.