我正在尝试将 UITextField 的占位符文本设置为不同的颜色。我了解到我需要子类化并覆盖 drawPlaceholderInRect 方法。
iPhone UITextField - Change placeholder text color
(void) drawPlaceholderInRect:(CGRect)rect {
[[UIColor blueColor] setFill];
[[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:16]];
}
这是我目前所拥有的,但我只是想不出如何让它恰到好处。我对最后一行感到困惑,因为我不知道如何将其映射到 MonoTouch/C# 对象。
using System;
using MonoTouch.UIKit;
using MonoTouch.Foundation;
using System.Drawing;
namespace MyApp
{
[Register("CustomUITextField")]
public class CustomUITextField:UITextField
{
public CustomUITextField () :base()
{
}
public CustomUITextField (IntPtr handle) :base(handle)
{
}
public override void DrawPlaceholder (RectangleF rect)
{
UIColor col = new UIColor(0,0,255.0,0.7);
col.SetFill();
//Not sure what to put here
base.DrawPlaceholder (rect);}
}
}
最佳答案
原始的 ObjC 代码没有调用 super
(这是基本方法)但是 drawInRect:
.您是否尝试过使用 MonoTouch 进行同样的操作?例如
public override void DrawPlaceholder (RectangleF rect)
{
using (UIFont font = UIFont.SystemFontOfSize (16))
using (UIColor col = new UIColor (0,0,255.0,0.7)) {
col.SetFill ();
base.DrawString (rect, font);
}
}
备注:drawInRect:WithFont:
映射到 DrawString
C# 中的扩展方法(可以在任何 string
上调用)。
关于c# - 在 Monotouch 中子类化和覆盖 UITextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12683853/