简体   繁体   中英

UITextField PlaceHolder Color change Monotouch

So I've been trying to change the color of UItextfield placeholder and I've had no luck. I've tried sub classing , and I've tried modifying the Attributed string but nothing works so hopefully someone out there can help me please.

This is the subclass i tired.

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;

Please can someone help me. Thanks in advanced.

Just simplify what you already have :-) This works on iOS7:

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

试试这个:

[yourtextField setValue:[UIColor blueColor] forKeyPath:@"_placeholderLabel.textColor"];

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM