2

Any ideas how to change color cursor in NSTextField for example

with UITextField the following code works

UITextField.appearance().tintColor = UIColor.redColor()

Any ideas?

sger
  • 719
  • 2
  • 12
  • 26
  • Could you post a screenshot or photoshopped image of what you are trying to achieve? The cursor is black and you can change the color of the _highlighting_ of text in System Preferences. – Arc676 Nov 04 '15 at 10:03
  • i want to change the cursor color – sger Nov 04 '15 at 10:12

1 Answers1

2

There's a couple ways to approach this problem.

1)

I spent some time just now writing a small sample project that customizes the cursor (which happens to be a color heart icon :-), and you can find it here.

It's based on the solutions found in this very related question.

2)

And then after doing this work, I realized that what you may simply want is to merely color an existing cursor. You can probably do that by implementing the setPointerColor:(NSColor *)newColor function found in this blog post on a NSCursor subclass that contains the cursor you want to colorize.

The important thing to remember is that you should use a subclassed NSTextField which keeps track of where the cursor is, so it can set the custom cursor or the cursor color when the mouse enters and moves around your text field.

That is, you'll need to override mouseEntered and possibly becomeFirstResponder as well, and include a tracking area to track the cursor as it crosses into your text field.

EDITED TO ADD

Turns out your question was talking about the CARET of the text field, which is the insertion point of the text and different from the mouse cursor.

Here is how you can change the color:

// change the insertion caret to another color
let fieldEditor = self.window?.fieldEditor(true, forObject: self) as! NSTextView

fieldEditor.insertionPointColor = NSColor.redColor()

The answer for which I found elaborated on here.

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215