美文网首页
textField更改占位文字颜色

textField更改占位文字颜色

作者: xdkoo | 来源:发表于2015-08-18 18:34 被阅读302次
    • 使用运行时、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更改占位文字颜色

          本文链接:https://www.haomeiwen.com/subject/qvfgqttx.html