问题
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?
回答1:
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 overridedrawPlaceholderInRect:
:
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.
来源:https://stackoverflow.com/questions/11479628/ios-how-to-set-textfield-hint-to-italic-with-regular-actual-text