iOS - Three ways to change the Placeholder color of UITextField

Sometimes, the color of the placeholder text that comes with UITextField is too light or does not meet the requirements, so it needs to be modified, and UITextField does not have a direct attribute to modify the color of the placeholder text, so it can only be modified by other indirect methods.


For example: the system default placeholder text color is too light 
write picture description here

Need to darken the color, or change the color 
Example: 
write picture description here


core code

Method 1: Modify the color of the placeholder text through the attributedPlaceholder attribute

    CGFloat viewWidth  = self.view.bounds.size.width;
    CGFloat textFieldX = 50;
    CGFloat textFieldH = 30;
    CGFloat padding    = 30;

    UITextField *textField = [[UITextField alloc] init];
    textField.frame = CGRectMake(textFieldX, 100, viewWidth - 2 * textFieldX, textFieldH);
    textField.borderStyle = UITextBorderStyleRoundedRect; // 边框类型
    textField.font = [UIFont systemFontOfSize:14];
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"请输入占位文字" attributes:
    @{NSForegroundColorAttributeName:[UIColor redColor],
                 NSFontAttributeName:textField.font
         }];
    textField.attributedPlaceholder = attrString;
    [self.view addSubview:textField];

Method 2: Modify the color of the placeholder text through KVC

    UITextField *textField1 = [[UITextField alloc] init];
    textField1.frame = CGRectMake(textFieldX, CGRectGetMaxY(textField.frame) + padding, viewWidth - 2 * textFieldX, textFieldH);
    textField1.borderStyle = UITextBorderStyleRoundedRect;
    textField1.placeholder = @"请输入占位文字";
    textField1.font = [UIFont systemFontOfSize:14];
    // "通过KVC修改占位文字的颜色"
    [textField1 setValue:[UIColor greenColor] forKeyPath:@"_placeholderLabel.textColor"];
    [self.view addSubview:textField1];

Method 3: Modify the color of the placeholder text by overriding the drawPlaceholderInRect:  method  of 

UITextField 


// 重写此方法
-(void)drawPlaceholderInRect:(CGRect)rect {
    // 计算占位文字的 Size
    CGSize placeholderSize = [self.placeholder sizeWithAttributes:
                              @{NSFontAttributeName : self.font}];

    [self.placeholder drawInRect:CGRectMake(0, (rect.size.height - placeholderSize.height)/2, rect.size.width, rect.size.height) withAttributes:
    @{NSForegroundColorAttributeName : [UIColor blueColor],
                 NSFontAttributeName : self.font}];
}

Summary: 
1. When we use pure code to create UITextField, it is most convenient to use the second method (KVC) to modify the color of the placeholder text. 
2. When we use XIB or Storyboard to create UITextField, modify the placeholder by customizing UITextField. Text color is the most suitable. 
3. We can also achieve this by combining the KVC modification attributes in the second method in the third rewriting method

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324661004&siteId=291194637