Monotouch中的子类化和重写UITextField
我正在尝试将UITextField的占位符文本设置为不同的颜色。 我已经了解到,我需要inheritance和重写drawPlaceholderInRect方法。
iPhone的UITextField – 改变占位符的文字颜色
(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:
映射到C#中的DrawString
扩展方法(可以在任何string
上调用)。