textField更改占位文字颜色

  • 使用运行时、KVC:
     unsigned int count = 0;
    Ivar *ivars = class_copyIvarList([UITextField class], &count);
    for (int i = 0; i < count; i++) {
        Ivar ivar = *(ivars + i);
        Ivar ivar = ivars[i];
        NSLog(@"%s",ivar_getName(ivar));
        
[self setValue:[UIColor whiteColor] forKeyPath:@"placeholderLabel.textColor"];
  • 使用富文本
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[NSForegroundColorAttributeName] = [UIColor whiteColor];
    NSAttributedString *str = [[NSAttributedString alloc] initWithString:self.placeholder attributes:dict];
    self.attributedPlaceholder = str;

  • 重写drawPlaceholderInRect方法
-(void)drawPlaceholderInRect:(CGRect)rect
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[NSForegroundColorAttributeName] = [UIColor whiteColor];
    CGRect newRect = CGRectMake(0, rect.size.height * 0.3, rect.size.width, rect.size.height);
    [self.placeholder drawInRect:newRect withAttributes:dict];
}

你可能感兴趣的:(textField更改占位文字颜色)