Subclassing and overriding UITextField in Monotouch

百般思念 提交于 2019-12-19 19:58:35

问题


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);}
    }
}

回答1:


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).



来源:https://stackoverflow.com/questions/12683853/subclassing-and-overriding-uitextfield-in-monotouch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!