在 Monotouch 中继承和覆盖 UITextField [英] Subclassing and overriding UITextField in Monotouch
问题描述
我正在尝试将 UITextField 的占位符文本设置为不同的颜色.我了解到我需要子类化并覆盖 drawPlaceholderInRect 方法.
iPhone UITextField - 更改占位符文本颜色
(void) drawPlaceholderInRect:(CGRect)rect {[[UIColor blueColor] setFill];[[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:16]];}
这是我到目前为止所拥有的,但我不知道如何正确使用它.我对最后一行感到困惑,因为我不知道如何将其映射到 MonoTouch/C# 对象.
使用系统;使用 MonoTouch.UIKit;使用 MonoTouch.Foundation;使用 System.Drawing;命名空间 MyApp{[注册(CustomUITextField")]公共类 CustomUITextField:UITextField{公共 CustomUITextField () :base(){}public CustomUITextField (IntPtr handle) :base(handle){}公共覆盖无效 DrawPlaceholder (RectangleF rect){UIColor col = new UIColor(0,0,255.0,0.7);col.SetFill();//不知道该放什么base.DrawPlaceholder(矩形);}}}
原始的 ObjC 代码不调用 super
(它的基本方法)而是 drawInRect:
.您是否尝试过使用 MonoTouch 进行相同操作?例如
public override void DrawPlaceholder (RectangleF rect){使用 (UIFont 字体 = UIFont.SystemFontOfSize (16))使用 (UIColor col = new UIColor (0,0,255.0,0.7)) {col.SetFill();base.DrawString(矩形,字体);}}
注意:drawInRect:WithFont:
映射到 C# 中的 DrawString
扩展方法(可以在任何 string
上调用).>
I am trying to set the placeholder text for a UITextField to a different color. I have learned that I need to subclass and override drawPlaceholderInRect method.
iPhone UITextField - Change placeholder text color
(void) drawPlaceholderInRect:(CGRect)rect {
[[UIColor blueColor] setFill];
[[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:16]];
}
Here is what I have so far, but I just can't figure out how to get it just right. I am confused on the last line as I don't know how to map this to MonoTouch/C# objects.
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);}
}
}
The original ObjC code does not call super
(it's base method) but drawInRect:
. Have you tried the same with MonoTouch ? e.g.
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);
}
}
Note: drawInRect:WithFont:
maps to the DrawString
extension method in C# (which can be called on any string
).
这篇关于在 Monotouch 中继承和覆盖 UITextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!