<!--
{
  "availability" : [
    "macOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSText/setTextColor(_:range:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSText(im)setTextColor:range:"
  },
  "title" : "setTextColor(_:range:)"
}
-->

# setTextColor(_:range:)

Sets the text color of characters within the specified range to the specified color.

```
func setTextColor(_ color: NSColor?, range: NSRange)
```

## Discussion

Removes the text color attribute if `color` is `nil`. This method applies only to rich text objects.

This method does not include undo support by default. Clients must invoke [`shouldChangeText(inRanges:replacementStrings:)`](/documentation/AppKit/NSTextView/shouldChangeText(inRanges:replacementStrings:)) or [`shouldChangeText(in:replacementString:)`](/documentation/AppKit/NSTextView/shouldChangeText(in:replacementString:)) to include this method in an undoable action.

---

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)