Xamarin iOS恢复PlaceHolder

时间:2018-03-27 15:19:43

标签: ios xamarin xamarin.ios

我有一个Xamarin iOS应用程序和一些文本字段,每个都有一个占位符,当它被选中时会被删除,我希望它在输入为空或空时恢复,我该如何获取它?

I tried that:

    public override void ViewWillAppear(bool animated)
    {
       base.ViewWillAppear(animated);
       string placeholderAux = txt.Placeholder;
       txt.EditingDidBegin += (object sender, EventArgs e) =>{
           if (string.IsNullOrWhiteSpace(txt.Text))
           {
              txt.Placeholder = placeholderAux;
           }
       };
}

但它不起作用

2 个答案:

答案 0 :(得分:1)

开始编辑时清除占位符。

textField.EditingDidBegin += (object sender, EventArgs e) =>
{
    (sender as UITextField).Placeholder = "";
};

当第一个响应者更改且字段为空时重置占位符:

textField.EditingDidEnd += (object sender, EventArgs e) =>
{
    var uITextField = sender as UITextField;
    if (string.IsNullOrEmpty(uITextField.Text))
    {
        uITextField.Placeholder = "StackOverflow";
    }
};

答案 1 :(得分:0)

尝试一下:

textField.AttributedPlaceholder = new NSAttributedString("place holder", foregroundColor: UIColor.FromRGB(204 ,204,204));