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

穿精又带淫゛_ 提交于 2019-12-22 10:34:01

问题


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



来源:https://stackoverflow.com/questions/11479628/ios-how-to-set-textfield-hint-to-italic-with-regular-actual-text

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