【问题标题】:UITextField PlaceHolder Color change MonotouchUITextField PlaceHolder 颜色改变 Monotouch
【发布时间】:2014-06-29 20:30:22
【问题描述】:

所以我一直在尝试更改 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;

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

【问题讨论】:

    标签: c# ios xamarin.ios uitextfield placeholder


    【解决方案1】:

    只需简化您已有的内容 :-) 这适用于 iOS7:

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

    【讨论】:

    • 非常感谢。这对我有帮助。我忘记了 NSAttributedString 的所有参数设置只需要前 3 个,并且可以将其余的设置为空。再次感谢
    • 同样的问题同样的解决方法不知道为什么,但是已经解决了!
    • 如果您只想更改占位符的 color 而不想为 text 创建字符串怎么办?有没有办法使用相同的 NSAttributedString 对象,但只是使第一个参数无效?
    • 我不明白你为什么要这么做。如果没有没有文字,为什么要设置它的颜色?
    • 这完全可行,我不知道为什么我不尝试。谢谢!
    【解决方案2】:

    这样做的唯一方法是(找到答案here

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

    【讨论】:

      【解决方案3】:

      试试这个:

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

      【讨论】:

      • 这是 ObjC。尽管它可能很容易翻译成 C#,但值得一提的是。
      猜你喜欢
      • 2016-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-28
      • 1970-01-01
      • 2011-01-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多