UITextField占位符颜色更改Monotouch

3
我一直在尝试更改UItextfield的占位符颜色,但是一直没有成功。我尝试了子类化和修改属性字符串,但都没有起作用。
这是我尝试过的子类:
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个回答

13

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

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

非常感谢。这帮助了我。我忘记了NSAttributedString只需要设置前3个参数,其余的可以设置为null。再次感谢。 - pjapple15
如果您只想更改占位符的颜色而不创建文本字符串,该怎么办?是否有一种方法可以使用相同的NSAttributedString对象,但只是将第一个参数置空? - Jimmy Amash
我不理解你为什么想要那样做。如果没有文本,你为什么想要设置它的颜色呢? - Krumelur
我的占位符文本已经设置好了吗?而且我需要在某个时候更改它的颜色,但不需要编写多余的代码,也就是相同的字符串? - Jimmy Amash
2
完全可以运行,我不知道为什么我没有尝试过。谢谢! - Jimmy Amash
显示剩余2条评论

0

这个问题的单调解决方式是(答案在这里找到)

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

-1

试试这个:

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

1
这是ObjC。虽然它可能很容易地转换成C#,但值得一提的是这个事实。 - Krumelur

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接