Search code examples
nstextfield

How to change insertion text (cursor) for NStextField? for os x swift 2


the most relevant code i found was let fieldEditor = self.window?.fieldEditor(true, forObject: textField) as! NSTextView? fieldEditor!.insertionPointColor = NSColor.redColor() but doesn't work because field editor is nil i tried this using in subclass of textfield.


Solution

  • Never mind found the answer by extending NSTextField class and calling the function manually

    extension NSTextField {
       public func customizeCaretColor(caretColor: NSColor) {
          let fieldEditor = self.window?.fieldEditor(true, forObject: self) as! NSTextView
          fieldEditor.insertionPointColor = caretColor
       }
    }