如何更改IOS Xamarin.Forms中浮动文本字段的默认占位符颜色
我在应用程序中使用了浮动文本字段。在中,我不知道如何更改ios中的默认占位符颜色。我附上了截图那里的密码默认颜色是灰色的,我必须改变为黑色的密码颜色。如果当时在条目中输入了任何文本,则只有密码颜色更改为黑色如何更改IOS Xamarin.Forms中浮动文本字段的默认占位符颜色,xamarin,xamarin.ios,Xamarin,Xamarin.ios,我在应用程序中使用了浮动文本字段。在中,我不知道如何更改ios中的默认占位符颜色。我附上了截图那里的密码默认颜色是灰色的,我必须改变为黑色的密码颜色。如果当时在条目中输入了任何文本,则只有密码颜色更改为黑色 原来问题是由MaterialEntryRenderer中的逻辑引起的 找到方法SetPlaceholderColor,它会更改placeholderColr呈现 修改如下: private void SetPlaceholderColor() { if (Element.Placeh
原来问题是由
MaterialEntryRenderer
中的逻辑引起的
找到方法SetPlaceholderColor
,它会更改placeholderColr呈现
修改如下:
private void SetPlaceholderColor()
{
if (Element.PlaceholderColor == Color.Default)
{
Control.FloatingLabelTextColor = _defaultPlaceholderColor;
Control.AttributedPlaceholder = new NSAttributedString(Control.Placeholder, new UIStringAttributes { ForegroundColor = _defaultPlaceholderColor });
}
else {
Control.FloatingLabelTextColor = Element.PlaceholderColor.ToUIColor();
Control.AttributedPlaceholder = new NSAttributedString(Control.Placeholder, new UIStringAttributes { ForegroundColor = Element.PlaceholderColor.ToUIColor() });
}
}
然后,您可以根据需要更改占位符颜色
var userNameEntry = new Entry()
{
Placeholder = "UserName",
PlaceholderColor = Color.Red,
TextColor = Color.Green,
};
var passwordEntry = new Entry()
{
Placeholder = "Password",
PlaceholderColor = Color.Black,
TextColor = Color.Gray,
};
您可能需要为条目创建自定义渲染器。您可以创建一个从iOS原始textField派生的类,例如:myTextField:UITextField,然后您可以添加自己的属性,还可以修改UITextField具有的占位符属性。之后,创建一个var txt=new myTextField(),感谢您的回复@Calvin Nunes。我正在使用自定义渲染器。它工作得很好。但我必须更改默认占位符的灰色。我对Appdelegate使用了下面的代码,但它没有反映。var t=new UITextField(){attributedpocholder=new NSAttributedString(“,null,UIColor.Red)};啊,对不起,我读得很快,让“颜色”这个词过去了。。。抱歉,这会更难,可能我帮不上忙,因为我对xamarin不是很熟练。你在用xamarin.Forms的入口控件吗?@Kevin Li,是的,我用过xamarin.Forms的入口控件