<!--
{
  "availability" : [
    "macOS: 12.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSTextSelection",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "AppKit"
    ],
    "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

[`-  initWithLocation:affinity:`](/documentation/AppKit/NSTextSelection/init(_:affinity:))

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

[`-  initWithRange:affinity:granularity:`](/documentation/AppKit/NSTextSelection/init(range:affinity:granularity:))

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

[`-  initWithRanges:affinity:granularity:`](/documentation/AppKit/NSTextSelection/init(_:affinity:granularity:))

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

[`-  initWithCoder:`](/documentation/AppKit/NSTextSelection/init(coder:))

Creates a test selection from data in an unarchiver.

### Characteristics of a selection

[`affinity`](/documentation/AppKit/NSTextSelection/affinity-swift.property)

Returns the selection affinity of the text selection.

[`Affinity`](/documentation/AppKit/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.

[`anchorPositionOffset`](/documentation/AppKit/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.

[`granularity`](/documentation/AppKit/NSTextSelection/granularity-swift.property)

The granularity of the selection.

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

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

[`logical`](/documentation/AppKit/NSTextSelection/isLogical)

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

[`transient`](/documentation/AppKit/NSTextSelection/isTransient)

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

[`secondarySelectionLocation`](/documentation/AppKit/NSTextSelection/secondarySelectionLocation)

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

[`NSTextLocation`](/documentation/AppKit/NSTextLocation)

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

[`textRanges`](/documentation/AppKit/NSTextSelection/textRanges)

Represents an array of noncontiguous logical ranges in the selection.

[`typingAttributes`](/documentation/AppKit/NSTextSelection/typingAttributes)

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

### Creating subselections

[`-  textSelectionWithTextRanges:`](/documentation/AppKit/NSTextSelection/textSelection(_:))

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

## Relationships

### Conforms To

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

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

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

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

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

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

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

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

### 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)