views:

443

answers:

3

I have a countdown timer textfield that displays 60 as a placeholder (grey). When I start the timer, it goes black and starts counting down. At the end, I invalidate the timer and set the countdown textfield back to 60. Of course, it's black now. How do I get it back to the placeholder text of 60 (meaning that it's grey) without resorting to a hack using NSAttributedString?

+5  A: 

Setting the TextField's text to nil or the empty string, @"". will cause the placeholder text to be displayed.

Erik
That was my original thought, too, but it didn't work
Walker Argendeli
Weird, according to the 10.5 docs:Placeholder strings, set using setPlaceholderString: or setPlaceholderAttributedString:, now appear in the text field cell if the actual string is nil or @"". They are drawn in grey on the cell and are not archived in the “pre-10.2” nib format.
Erik
Walker Argendeli: What was the code you used to do that?
Peter Hosey
Erik: I can't find any mention of setPlaceholderString in the docs... Peter: [countdownTextField setStringValue:nil];
Walker Argendeli
NSTextFieldCell implements setPlaceholderString, and NSTextField in turn has an embedded NSTextFieldCell. I was setting a NSTextField's contents to empty using [textField setStringValue:@""].
Erik
A: 

Since I don't have enough reputation to comment, but are you looking to change the font color to grey or hide the label?

JuniorFlip
I'm looking to change the font color to grey.
Walker Argendeli
And yes, I realize that doing so would be as simple as [countdownTextField setTextColor:grey];It's just that that's kind of hacky.
Walker Argendeli
+1  A: 

If you are using the "value" binding on the NSTextField, you will need to set the Null Placeholder for the binding instead of for the text cell's placeholder. The empty string (@"" ) does seem to count as a null value for the purposes of the placeholder.

natevw