use*_*500 5 c# objective-c uitextfield xamarin.ios ios
我正在尝试将UITextField的占位符文本设置为其他颜色。我了解到我需要继承并重写drawPlaceholderInRect方法。
(void) drawPlaceholderInRect:(CGRect)rect {
[[UIColor blueColor] setFill];
[[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:16]];
}
Run Code Online (Sandbox Code Playgroud)
这是到目前为止的内容,但是我无法弄清楚如何正确处理它。最后一行让我感到困惑,因为我不知道如何将其映射到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);}
}
}
Run Code Online (Sandbox Code Playgroud)
原始的 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);
}
}
Run Code Online (Sandbox Code Playgroud)
注意:drawInRect:WithFont:映射到DrawStringC# 中的扩展方法(可以在任何 上调用string)。