子类和压倒一切的MonoTouch的UITextField [英] Subclassing and overriding UITextField in Monotouch
问题描述
我想对UITextField的占位符文本设置为不同的颜色。我了解到,我需要继承并重写drawPlaceholderInRect方法。
的 iPhone的UITextField - 更改占位符文本颜色
(无效)drawPlaceholderInRect :(的CGRect){RECT
[[的UIColor blueColor] setFill]
[[自我占位符] drawInRect:RECT withFont:[UIFont systemFontOfSize:16];
}
下面是我到目前为止,但我只是想不通如何得到它恰到好处。我很困惑的最后一行,因为我不知道如何将这种映射到的MonoTouch / C#对象
使用系统;
使用MonoTouch.UIKit;
使用MonoTouch.Foundation;
使用System.Drawing中;
命名空间MyApp的
{
[注册(CustomUITextField)]
公共类CustomUITextField:的UITextField
{
公共CustomUITextField():基地()
{
}
公共CustomUITextField(IntPtr的手柄):基地(句柄)
{
}
公共覆盖无效DrawPlaceholder(矩形的RectangleF)
{
的UIColor山坳=新的UIColor(0,0,255.0,0.7);
col.SetFill();
//不知道要放什么东西在这里
base.DrawPlaceholder(RECT);}
}
}
原来ObjC代码不叫超级
(它的基本方法),但 drawInRect:
。您是否尝试过使用MonoTouch的一样吗?例如:
公共覆盖无效DrawPlaceholder(矩形的RectangleF)
{
使用(UIFont的字体= UIFont.SystemFontOfSize (16))使用(COL的UIColor =新的UIColor(0,0,255.0,0.7)){
col.SetFill
();
base.DrawString(RECT,字体);
}
}
请注意: drawInRect:WithFont
映射到C#中的的DrawString
扩展方法(这可以在任何字符串被称为
)。
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屋!