iOS - How to set textfield hint to italic (with regular actual text)?

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.

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?

Answers


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:

And that's it. Enjoy.


Need Your Help

Binding a redux store to a function

javascript reactjs redux

I have these routes that I want wrapped into a checkAuth method to see the session state of the visitor. To keep the code clean I separated the checkAuth method to a separate file and imported it i...

Which properties should be used with CSSearchableItemAttributeSet on NSUserActivity?

ios corespotlight ios-searchapi nsuseractivity

I added Web Markup to my website so items will appear in Spotlight Search results when users search in iOS 9. Users can browse the same items in the app, therefore I want to create NSUserActivity o...