修改UITextField的placeholder文字显示样式

写在前面: 在日常编程中经常有需要修改UITextField的placeholder的显示样式,每次做法都可能不太一样,实现的复杂程度也可能存在很大的差别,为了能在这个需求上不再浪费时间和精力去完成,下面总结了集中自认为比较简便的方式去实现,大致有四种方式如下:
  1. 方式一

    • 通过富文本NSAttributedString进行设置
    // 属性字典
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
    // 创建富文本字符串对象
    NSAttributedString *placeholder = [[NSAttributedString alloc] initWithString:@"请输入手机号" attributes:attrs];
    // 为textfield对象设置占位文字
    self.phoneField.attributedPlaceholder = placeholder;
    
  2. 方式二

    • 通过NSMutableAttributedString进行设置
    • NSMutableAttributedString相对于NSAttributedString的有点在于可以设置任意位置的文字属性,原因是多了一个下面的方法:
    // 这个方法能设置不同位置的文字属性
    -(void)setAttributes:(nullable NSDictionary *)attrs range:(NSRange)range;
    
    • 下面展示设置textField的占位文字颜色
    NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:@"请输入手机号"];
    
    [placeholder setAttributes:@{NSForegroundColorAttributeName:[UIColor redColor],
    NSFontAttributeName:[UIFont systemFontOfSize:10]
    } range:NSMakeRange(0, 1)];
    [placeholder setAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]
                                 } range:NSMakeRange(1, 2)];
    self.phoneField.attributedPlaceholder = placeholder;
    
  • 方式三

    • 写一个LJTextField继承自UITextField,并重写下面的方法就可以了:
    // 重写这个方法,在方法内部重新画placeholder的样式
    -(void)drawPlaceholderInRect:(CGRect)rect;
    
    
    • 例如:
        #import "LJTextField.h"
    
        @implementation LJTextField
    
        - (void)drawPlaceholderInRect:(CGRect)rect
        {   // 拿到自己的placeholder设置传入设置的范围和文字的属性字典就可以了
            [self.placeholder drawInRect:self.frame withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10]}];
        }
    
        @end
    
  1. 方式四

    • 利用运行时获取UITextField隐藏的属性,再用KVC为其赋值

    • 利用运行时技术:先简单的科普一下运行时吧!

      • 运行时(又称Runtime)
      • 是苹果官方一套C语言库
      • 能做很多底层的操作,如:访问隐藏的一些成员变量/成员方法;
    • 获取UITextField的隐藏属性

      • 导入头文件
        #import 
        
      • 写入下面的代码

      // 在这个方法里面利用运行时找到想要隐藏式属性并打印
      +(void)initialize{
      unsigned int count = 0;
      // 拷贝出所有的成员变量列表
      Ivar *ivars = class_copyIvarList([UITextField class], &count);

      for (int i = 0; i // 取出成员变量
      // Ivar ivar = *(ivars + i);
      Ivar ivar = ivars[i];

        // 打印成员变量名字
        XMGLog(@"%s %s", ivar_getName(ivar), ivar_getTypeEncoding(ivar));
      

      }
      // 释放
      free(ivars);
      }
      ```

      • 在初始化方法中利用KVC为隐藏的成员变量赋值就可以了
       - (void)awakeFromNib
      {
      // 分开写 修改占位文字颜色
       //UILabel *placeholderLabel = [self valueForKeyPath:@"_placeholderLabel"];
      placeholderLabel.textColor = [UIColor redColor];
      //合并写
      [self setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];
      // 设置光标颜色和文字颜色一致
      self.tintColor = self.textColor;
      
      }
      

总结:

  • 上面的四种思路均可以改变UITextField的占位文字颜色,提供了不同的选择,但是placeholder的文字颜色的设置方式,绝对不止上面的集中方式,如果有更多的,简便的方式,还希望大家能够互相交流.

你可能感兴趣的:(修改UITextField的placeholder文字显示样式)