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];
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容