iOS修改placeholderLabel属性的文字颜色

项目中因为需要修改UITextField控件的提示信息颜色,感觉蛮有意思的就记录一下分享给小伙伴们。本次项目使用的是OC,使用Swift的童鞋请自行破解。O(∩_∩)O哈哈~

一、清风明月,无废话

注意引入库文件#import "objc/runtime.h"

具体代码如下:

//
//  ViewController.m
//  MyBoy
//
//  Created by 叶糖糖 on 2020/5/20.
//  Copyright © 2020 SugarYe. All rights reserved.
//

#import "ViewController.h"
#import "objc/runtime.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UITextField *textField = [[UITextField alloc] init];
    textField.placeholder = @"请出入你的手机号";
    textField.backgroundColor = [UIColor grayColor];
    textField.frame = CGRectMake(40, 80, 300, 30);
    
    Ivar ivar = class_getInstanceVariable([UITextField class], "_placeholderLabel");
    UILabel *placehoderLabel = object_getIvar(textField,ivar);
    placehoderLabel.textColor = [UIColor whiteColor];

    
    [self.view addSubview:textField];
}


@end

总结

好了,小伙伴们!恭喜你,又搞定了一个小黑科技!