Cocoa NSTextField change placeholder color

Images
Related searches

I try to change the placeholder text color. This code doesn't work:

let color = NSColor.redColor()
let attrs = [NSForegroundColorAttributeName: color]
let placeHolderStr = NSAttributedString(string: "My placeholder", attributes: attrs)
myTextField.placeholderAttributedString = placeHolderStr

I get the error -[NSTextField setPlaceholderAttributedString:]: unrecognized selector sent to instance. Any ideas, how I can change the color of the placeholder?

UPDATE: This works:

(myTextField.cell() as NSTextFieldCell).placeholderAttributedString = placeHolderStr

UPDATE 2: Hmm, it changes the color, but if the text field gets the focus, the placeholder font size get's smaller, very strange.

By explicitly defining the font of the NSAttributedString, the placeholder font resizing referred to in the original question is fixed.

The following is a working example in Swift 3.0.

let color = NSColor.red
let font = NSFont.systemFont(ofSize: 14)
let attrs = [NSForegroundColorAttributeName: color, NSFontAttributeName: font]
let placeholderString = NSAttributedString(string: "My placeholder", attributes: attrs)
(textField.cell as? NSTextFieldCell)?.placeholderAttributedString = placeholderString

The following is a working example in Swift 4.2.

let attrs = [NSAttributedString.Key.foregroundColor: NSColor.lightGray,
             NSAttributedString.Key.font: NSFont.systemFont(ofSize: 14)]
let placeholderString = NSAttributedString(string: "My placeholder", attributes: attrs)
(taskTextField.cell as? NSTextFieldCell)?.placeholderAttributedString = placeholderString

NSTextFieldCell, The NSTextFieldCell class adds to the text display capabilities of the NSCell class by allowing you to set the color of both the text and its background. are also declared by the NSTextField class, which uses NSTextFieldCell objects to draw and edit text. The placeholder text for the cell, specified as a plain text string. Change NSTextField border and BG color while editing Tag: osx , swift , cocoa , nstextfield I have a NSTextField that uses no border and window background color while it is displayed but I want it to change to have the default border and white BG color when being edited.

You should set the placeholder text in NSTextFieldCell and not NSTextField.

myTextField.cell.placeholderAttributedString = placeHolderStr

Styling NSTextField: A Guide for Designers | by John Marstall, The NSTextField class exposes only a handful of cosmetic controls, and A text field styled with CSS … you'd write the following CSS: input { We might also like to customize the color of any placeholder text in the field. How to change NSTextField background color in NSPopOver. Tag: objective-c,osx,cocoa. Mac OSX 10.10 Xcode 6.1. I created a tableview in NSPopOver. I try to change

You should keep current font, and current value from IB

  extension NSTextField {

    func setHintTextColor (color: NSColor) {
        let currentHint = placeholderString ?? ""
        let placeholderAttributes: [NSAttributedString.Key: Any] = [
            NSAttributedString.Key.foregroundColor: color,
            NSAttributedString.Key.font: font!
        ]

        let placeholderAttributedString = NSMutableAttributedString(string: currentHint, attributes: placeholderAttributes)
        let paragraphStyle = NSMutableParagraphStyle()

        placeholderAttributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0,length: placeholderAttributedString.length))

        self.placeholderAttributedString =  placeholderAttributedString
    }
}

CueText Color - General, It does seem possible to change the placeholder color in NSTextField : http:// stackoverflow.com/questions/25106372/cocoa-nstextfield-change-placeholder-� In most browsers, the placeholder text is grey. To change this, style the placeholder with the non-standard ::placeholder selector. Note that Firefox adds a lower opacity to the placeholder, so we use opacity: 1 to fix this.

swift - Cocoa NSTextField change placeholder color -, I try to change the color of the placeholder text. This code does not work: Go color = NSColor.redColor () go attrs� Change the color of top border with CSS; Change the color of right border with CSS; Change the Color of Active Links with CSS; Not showing a placeholder for input type=“date” field with HTML5. How to solve it? Add a background color to the form input with CSS; jQuery .val change doesn't change input value? Change the color of the bottom

NSTextField text and background colour with/without focus, Updates: Changing the color did not change the aforementioned behavior. The text color does not change at any time during this process. Placeholder text also� For developer guidance, see NSTextField. Data Entry. Use an introductory label or placeholder text to help communicate purpose. A label helps the user understand what type of information they should enter. A text field can also contain placeholder text—such as Email or Password—when there’s no other text in the field. A label is often

Overview. The NSText View class is the front-end class to the AppKit text system. The class draws the text managed by the back-end components and handles user events to select and modify its text, in addition to supporting rich text, attachments, input management and key binding, and marked text attributes.

Comments
  • Though it's not ideal, you can always add an additional attribute for setting the font (NSFontAttributeName), which will prevent the placeholder text from showing smaller than desired.
  • Hi, Lupurus, have you found a solution for the smaller font size when the placeholder gets focus? I'm having the same issue.
  • Seeing the same and wondering how to fix it, although the placeholder goes away when you don't play with the settings @Daniel
  • Thanks for your answer, but there is no cell property at the NSTextField