3

Is there any easy way to set a text field's hint to italic, while keeping the actual text non-italicized? The image below shows what I'm trying to accomplish, but doesn't actually work, as text typed into the first text field is still italicized.

text fields

If there's no easy way to do this, I'm thinking that I'll have to implement methods on each of the text fields to check the length of the text, and if zero apply the italic font. This would work, right? Is there a better way?

Kyle Clegg
  • 38,547
  • 26
  • 130
  • 141

1 Answers1

5

Try this:

UIFont* italicFont = [UIFont italicSystemFontOfSize:[UIFont systemFontSize]];    
[yourTextField setValue:italicFont forKeyPath:@"_placeholderLabel.font"];

EDIT:

Ok, here is another (legal) way to do it:

  • 1.Subclass UITextField and override drawPlaceholderInRect::

TextFieldSubclass.h:

@interface TextFieldSubclass : UITextField

@end

TextFieldSubclass.m:

- (void) drawPlaceholderInRect:(CGRect)rect 
{
    [[UIColor lightGrayColor] setFill];
    UIFont* italicFont = [UIFont italicSystemFontOfSize:[UIFont systemFontSize]];
    [[self placeholder] drawInRect:rect withFont:italicFont];
}
  • 2.Change your UITextField class and set it to TextFieldSubclass:

enter image description here

And that's it. Enjoy.

Xavi Gil
  • 11,460
  • 4
  • 56
  • 71