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
2009-06-23 14:52:26
That was my original thought, too, but it didn't work
Walker Argendeli
2009-06-23 15:23:20
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
2009-06-23 15:43:33
Walker Argendeli: What was the code you used to do that?
Peter Hosey
2009-06-23 16:58:01
Erik: I can't find any mention of setPlaceholderString in the docs... Peter: [countdownTextField setStringValue:nil];
Walker Argendeli
2009-06-24 16:44:20
NSTextFieldCell implements setPlaceholderString, and NSTextField in turn has an embedded NSTextFieldCell. I was setting a NSTextField's contents to empty using [textField setStringValue:@""].
Erik
2009-06-24 19:47:47
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
2009-06-24 12:52:22
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
2009-06-24 16:38:57
+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
2010-02-26 19:19:19