UITextField PlaceHolder颜色变化Monotouch

时间:2014-05-12 12:56:11

标签: c# ios xamarin.ios uitextfield placeholder

所以我一直试图改变UItextfield占位符的颜色,但我没有运气。我已经尝试过子类,我已经尝试修改Attributed字符串,但没有任何作用,所以希望有人可以帮助我。

这是我厌倦的子类。

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


}

这是我尝试的属性字符串方法:

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


            ForegroundColor = UIColor.White.CGColor,StrokeColor = UIColor.White.CGColor,
            StrokeWidth = 5.0f
        });
        this.emailtextbox.AttributedPlaceholder = textat;

请有人帮助我。谢谢你提前。

3 个答案:

答案 0 :(得分:12)

简化你已有的东西:-) 这适用于iOS7:

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

答案 1 :(得分:0)

试试这个:

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

答案 2 :(得分:0)

这样做的一种方式是(答案是here

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