1. 程式人生 > >ios_UITextField-修改佔位文字和游標的顏色,大小

ios_UITextField-修改佔位文字和游標的顏色,大小

一.設定佔位文字的顏色

方法一:利用富文字

/** 手機號輸入框 */
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
 
- (void)viewDidLoad {
    [super viewDidLoad];
    // 建立一個富文字物件
    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    // 設定富文字物件的顏色
    attributes[NSForegroundColorAttributeName] = [UIColor whiteColor];
    // 設定UITextField的佔位文字
    self.phoneTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString
:@"手機號" attributes:attributes]; }

方法二:利用Runtime獲取私有的屬性名稱,利用KVC設定屬性

// 設定佔位文字的顏色為紅色(注意下面的'self'代表你要修改佔位文字的UITextField控制元件)
[self setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
  • 注意:_placeholderLabel.textColor是不可亂寫的哦,我們是怎麼獲取到這個屬性的呢?請看下文:
    // 只調用一次(自定義UITextField)
    + (void)initialize 
    { [self getIvars]; } // 獲取私有變數名稱 + (void)getIvars { unsigned int count = 0; Ivar *ivars = class_copyIvarList([UITextField class], &count); for (int i = 0; i < count; i++) { Ivar ivar = ivars[i]; NSLog(@"%s----%s", ivar_getName(ivar), ivar_getTypeEncoding(ivar));
    } }

    檢視列印,找出可能的屬性名稱,試試便知;

    • 完整程式碼:自定義的UITextField,獲取到焦點(編輯狀態)的時候是白色,失去焦點(非編輯狀態)的時候是灰色:
      #import "YCTextField.h"
      #import <objc/runtime.h>
       
      #define YCplaceholderTextColor @"_placeholderLabel.textColor"
       
      @implementation YCTextField
       
      + (void)initialize {
       
          [self getIvars];
       
      }
       
      // 獲取私有變數名稱
      + (void)getIvars {
       
          unsigned int count = 0;
       
          Ivar *ivars = class_copyIvarList([UITextField class], &count);
       
          for (int i = 0; i < count; i++) {
              Ivar ivar = ivars[i];
       
              NSLog(@"%s----%s", ivar_getName(ivar), ivar_getTypeEncoding(ivar));
          }
      }
       
      - (void)awakeFromNib {
       
          // 設定游標的顏色
          self.tintColor = self.textColor;
      }
       
      // 獲取到焦點
      - (BOOL)becomeFirstResponder {
       
          // 利用執行時獲取key,設定佔位文字的顏色
          [self setValue:self.textColor forKeyPath:YCplaceholderTextColor];
       
          return [super becomeFirstResponder];
      }
       
      // 失去焦點
      - (BOOL)resignFirstResponder {
       
          // 利用執行時獲取key,設定佔位文字的顏色
          [self setValue:[UIColor grayColor] forKeyPath:YCplaceholderTextColor];
       
          return [super resignFirstResponder];
      }
       
      @end

      方法三.將佔位文字上去(重寫- (void)drawPlaceholderInRect:(CGRect)rect;)

      - (void)drawPlaceholderInRect:(CGRect)rect
      {
       
          [[UIColor orangeColor] set];
       
          [self.placeholder drawInRect:rect withFont:[UIFont systemFontOfSize:20]];
      }

      二.設定游標顏色

      // 設定游標的顏色
      self.tintColor = [UIColor redColor];

      三.設定佔位文字的偏移

      • 重寫-(CGRect)placeholderRectForBounds:(CGRect)bounds;方法
      • 可以用來設定游標與佔位的間距

        //控制placeHolder的位置,左右縮20
        -(CGRect)placeholderRectForBounds:(CGRect)bounds
        {
         
          //return CGRectInset(bounds, 20, 0);
          CGRect inset = CGRectMake(bounds.origin.x+50, bounds.origin.y, bounds.size.width -10, bounds.size.height);//更好理解些
          return inset;
        }
        擴充:系統還提供了很多類似的方法
        – textRectForBounds:  //重寫來重置文字區域
        – drawTextInRect:    //改變繪文字屬性.重寫時呼叫super可以按預設圖形屬性繪製,若自己完全重寫繪製函式,就不用呼叫super了.
        – placeholderRectForBounds:  //重寫來重置佔位符區域
        – drawPlaceholderInRect:  //重寫改變繪製佔位符屬性.重寫時呼叫super可以按預設圖形屬性繪製,若自己完全重寫繪製函式,就不用呼叫super了
        – borderRectForBounds:  //重寫來重置邊緣區域
        – editingRectForBounds:  //重寫來重置編輯區域
        – clearButtonRectForBounds:  //重寫來重置clearButton位置,改變size可能導致button的圖片失真
        – leftViewRectForBounds:
        – rightViewRectForBounds:

        原文連結:https://blog.csdn.net/u012907783/article/details/53157685