3

I have been trying to change the color of UItextfield placeholder and I have had no luck. I have tried sub classing, and I have tried modifying the Attributed string but nothing worked.

This is the subclass I tried.

public class CustomTextbox : UITextField
{
    private string placeholder{ get;  set; }
    public CustomTextbox ()
    {
    }

    public CustomTextbox(string theString)
    {
        this.placeholder = theString;
    }

    public override void DrawPlaceholder (RectangleF rect) {
        using (UIFont font = UIFont.SystemFontOfSize (16)) 
        using (UIColor col = new UIColor (UIColor.Blue.CGColor)) { 
            col.SetFill (); col.SetStroke (); col.SetColor (); base.DrawString (base.Placeholder,rect, font); } }
            

}

This is the attributed string method I tried:

var textat = new NSMutableAttributedString ("Email", new CTStringAttributes () {

            
            ForegroundColor = UIColor.White.CGColor,StrokeColor = UIColor.White.CGColor,
            StrokeWidth = 5.0f
        });
        this.emailtextbox.AttributedPlaceholder = textat;
vvvvv
  • 25,404
  • 19
  • 49
  • 81
pjapple15
  • 299
  • 4
  • 10

3 Answers3

13

Just simplify what you already have. This works on iOS7:

var t = new UITextField()
{
  AttributedPlaceholder = new NSAttributedString("some placeholder text", null, UIColor.Red)
}
vvvvv
  • 25,404
  • 19
  • 49
  • 81
Krumelur
  • 32,180
  • 27
  • 124
  • 263
  • Thanks so much . This helped me. i forgot that NSAttributedString had all its parameters set only really needed the first 3 and could leave the rest to be set to null. Thanks again – pjapple15 May 14 '14 at 06:16
  • Same problem and same solution do not know why, but fixed it! – Mitchell Currie Nov 06 '15 at 04:44
  • What if you only want to change the _color_ of the placeholder and not create a string for the _text_? Is there a way to use the same NSAttributedString object but just nullify the first argument? – Jimmy Amash Jul 12 '17 at 22:50
  • I don't understand why you would want to do that. If there is _no text_, why would you want to set its color? – Krumelur Jul 13 '17 at 06:45
  • My placeholder text is already set? And I need to change its color at some point without writing extra code I don't need, ie. the same string? – Jimmy Amash Jul 14 '17 at 01:17
  • Well, read the current placeholder value, create an `NSAttributedString` from it with your color and set it back. – Krumelur Jul 14 '17 at 06:07
  • 2
    That totally works and I have no idea why I didn't try that. Thanks! – Jimmy Amash Jul 14 '17 at 14:19
0

The monotuch way to do this is (the answer is found here)

myLogin.AttributedPlaceholder = new NSAttributedString (
    "Enter your credentials",
    font: UIFont.FromName ("HoeflerText-Regular", 24.0f),
    foregroundColor: UIColor.Red,
    strokeWidth: 4 
);
Tom
  • 21
  • 5
-1

Try this one:

[yourtextField setValue:[UIColor blueColor] forKeyPath:@"_placeholderLabel.textColor"];
Soumya Ranjan
  • 4,817
  • 2
  • 26
  • 51
  • 1
    This is ObjC. And even though it can probably be translated easily into C#, it would be worth mentioning that fact. – Krumelur May 13 '14 at 07:52