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;
有帮助吗?

解决方案

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

var t = new UITextField()
{
  AttributedPlaceholder = new NSAttributedString("some placeholder text", null, UIColor.Red)
}

其他提示

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 
);

Try this one:

[yourtextField setValue:[UIColor blueColor] forKeyPath:@"_placeholderLabel.textColor"];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top