iOS - 如何将文本字段提示设置为斜体(带有常规实际文本)? [英] iOS - How to set textfield hint to italic (with regular actual text)?

查看:27
本文介绍了iOS - 如何将文本字段提示设置为斜体(带有常规实际文本)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何简单的方法可以将文本字段的提示设置为斜体,同时保持实际文本为非斜体?下图显示了我想要完成的工作,但实际上并没有奏效,因为在第一个文本字段中输入的文本仍然是斜体.

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?

推荐答案

试试这个:

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

<小时>

好的,这是另一种(合法的)方法:

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

  • 1.Subclass UITextField 并覆盖 drawPlaceholderInRect::
  • 1.Subclass UITextField and override drawPlaceholderInRect::

TextFieldSubclass.h:

TextFieldSubclass.h:

@interface TextFieldSubclass : UITextField

@end

TextFieldSubclass.m:

TextFieldSubclass.m:

- (void) drawPlaceholderInRect:(CGRect)rect 
{
    [[UIColor lightGrayColor] setFill];
    UIFont* italicFont = [UIFont italicSystemFontOfSize:[UIFont systemFontSize]];
    [[self placeholder] drawInRect:rect withFont:italicFont];
}

  • 2. 更改您的 UITextField 类并将其设置为 TextFieldSubclass:
    • 2.Change your UITextField class and set it to TextFieldSubclass:
    • 就是这样.享受.

      这篇关于iOS - 如何将文本字段提示设置为斜体(带有常规实际文本)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆