<!--
{
  "availability" : [
    "iOS: 15.0.0 -",
    "iPadOS: 15.0.0 -",
    "macCatalyst: 15.0.0 -",
    "tvOS: 15.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "UIKit",
  "identifier" : "/documentation/UIKit/NSTextSelection",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "UIKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSTextSelection"
  },
  "title" : "NSTextSelection"
}
-->

# NSTextSelection

A class that represents a single logical selection context that corresponds to an insertion point.

```
class NSTextSelection
```

## Topics

### Creating a text selection

[`convenience init(any NSTextLocation, affinity: NSTextSelection.Affinity)`](/documentation/UIKit/NSTextSelection/init(_:affinity:))

Creates a new text selection with the location and selection affinity you provide.

[`convenience init(range: NSTextRange, affinity: NSTextSelection.Affinity, granularity: NSTextSelection.Granularity)`](/documentation/UIKit/NSTextSelection/init(range:affinity:granularity:))

Creates a new text selection with the range, selection affinity, and granularity you provide.

[`init([NSTextRange], affinity: NSTextSelection.Affinity, granularity: NSTextSelection.Granularity)`](/documentation/UIKit/NSTextSelection/init(_:affinity:granularity:))

Creates a new text selection with the ranges, selection affinity, and granularity you provide.

[`init?(coder: NSCoder)`](/documentation/UIKit/NSTextSelection/init(coder:))

Creates a test selection from data in an unarchiver.

### Characteristics of a selection

[`var affinity: NSTextSelection.Affinity`](/documentation/UIKit/NSTextSelection/affinity-swift.property)

Returns the selection affinity of the text selection.

[`enum Affinity`](/documentation/UIKit/NSTextSelection/Affinity-swift.enum)

Values that describe the visual location of the text cursor, or the direction of the non-anchored edge of the selection.

[`var anchorPositionOffset: CGFloat`](/documentation/UIKit/NSTextSelection/anchorPositionOffset)

Represents the anchor position offset from the beginning of a line fragment in the visual order for the initial tap or click location.

[`var granularity: NSTextSelection.Granularity`](/documentation/UIKit/NSTextSelection/granularity-swift.property)

The granularity of the selection.

[`enum Granularity`](/documentation/UIKit/NSTextSelection/Granularity-swift.enum)

Values that describe the different granularities available to make a selection.

[`var isLogical: Bool`](/documentation/UIKit/NSTextSelection/isLogical)

A Boolean value that indicates whether the framework interprets the selection as logical or visual.

[`var isTransient: Bool`](/documentation/UIKit/NSTextSelection/isTransient)

A Boolean value that indicates transient text selection during drag handling.

[`var secondarySelectionLocation: (any NSTextLocation)?`](/documentation/UIKit/NSTextSelection/secondarySelectionLocation)

Specifies the secondary character location when user taps or clicks at a directional boundary.

[`protocol NSTextLocation`](/documentation/UIKit/NSTextLocation)

An interface you implement that represents an abstract location inside your document’s content.

[`var textRanges: [NSTextRange]`](/documentation/UIKit/NSTextSelection/textRanges)

Represents an array of noncontiguous logical ranges in the selection.

[`var typingAttributes: [NSAttributedString.Key : Any]`](/documentation/UIKit/NSTextSelection/typingAttributes)

The template attributes the framework uses for characters that replace the contents of this selection.

### Creating subselections

[`func textSelection([NSTextRange]) -> NSTextSelection`](/documentation/UIKit/NSTextSelection/textSelection(_:))

Creates a subselection of the current text selection with the ranges you specify.

### Initializers

[`convenience init(location: any NSTextLocation, affinity: NSTextSelection.Affinity)`](/documentation/UIKit/NSTextSelection/init(location:affinity:))

[`init(ranges: [NSTextRange], affinity: NSTextSelection.Affinity, granularity: NSTextSelection.Granularity)`](/documentation/UIKit/NSTextSelection/init(ranges:affinity:granularity:))

## Relationships

### Conforms To

[`Hashable`](/documentation/Swift/Hashable)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`CVarArg`](/documentation/Swift/CVarArg)

[`NSSecureCoding`](/documentation/Foundation/NSSecureCoding)

[`NSCoding`](/documentation/Foundation/NSCoding)

[`Equatable`](/documentation/Swift/Equatable)

### Inherits From

[`NSObject-swift.class`](/documentation/ObjectiveC/NSObject-swift.class)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)