5

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);}
    }
}
Community
  • 1
  • 1
user1060500
  • 1,505
  • 1
  • 21
  • 40

1 Answers1

2

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

poupou
  • 43,413
  • 6
  • 77
  • 174
  • Thank you. I will try this and let you know. You solved the missing piece of the puzzle I think, which was I wasn't sure what drawInRect:WithFont: mapped to. Now that I know, I will give it a try. – user1060500 Oct 02 '12 at 14:48
  • I tried this. And I can get my code to run. Breakpoint is being hit ok when the control is instantiated. The problem is that it doesn't do anything. The control never updates no matter what I do. The placeholder text isn't shown. I tried changing the background colour of the control, and the code gets hit, but nothing changes in the UI..... hmmm any thoughts? In XCode, I've set the Class to my custom class. so no problem there (plus breakpoint is hit). – user1060500 Oct 06 '12 at 04:21
  • I translated the ObjC code you provided to C# but I did not try it (not in ObjC/Xcode or C#/MonoTouch). However, from the original link, it seems that does not work for everyone. If you have a working example in ObjC then file a bug report http://bugzilla.xamarin.com and include it, along with your C# port, and we'll have a look at it. – poupou Oct 06 '12 at 14:26
  • I can't find a DrawString extension method for a String. Maybe I am missing an import, but I can't see anywhere in .NET documentation about DrawString being an extension method for a String. It's an extension method of a graphics object, for sure - which string is not. Are you sure about DrawString? – user1060500 Oct 06 '12 at 14:32
  • Everything works now! I had to change your call to DrawString to be called from base. Thanks a lot for your help. I will modify your entry with a modification to the DrawString call you made. public override void DrawPlaceholder (RectangleF rect) { using (UIFont font = UIFont.SystemFontOfSize (16)) using (UIColor col = new UIColor (0,0,255,(float)0.7)) { col.SetFill (); base.DrawString (base.Placeholder,rect, font); } } – user1060500 Oct 06 '12 at 14:53