Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextField set placeholder color as dark in iOS

I have tried to set UITextField "Placeholder" color as dark.

NSAttributedString * search = [[NSAttributedString alloc] initWithString:@"Search" attributes:@{NSForegroundColorAttributeName: [UIColor blackColor]}];

textField.attributedPlaceholder = search;
  • But still UITextField showing light gray color in "Placeholder".

  • Is it possible to set dark "placeholder" color for UITextField?

Also I Have tried the another method as well

[textField setValue:[UIColor blackColor] forKeyPath:@"_placeholderLabel.textColor"];

But both methods are working in iOS 7, But not working on iOS 6.

  • Is it possible to set dark "placeholder" color for UITextField in iOS 6 target?

Thanks!

like image 243
Natarajan Avatar asked Apr 19 '14 04:04

Natarajan


2 Answers

As suggested by Apple, subclassing UITextField and overriding - (void)drawPlaceholderInRect:(CGRect)rect is the way to go:

- (void)drawPlaceholderInRect:(CGRect)rect { 
    UIColor *colour = [UIColor lightGrayColor]; 
    if ([self.placeholder respondsToSelector:@selector(drawInRect:withAttributes:)]) 
    { // iOS7 and later 
        NSDictionary *attributes = @{NSForegroundColorAttributeName: colour, NSFontAttributeName: self.font}; 
        CGRect boundingRect = [self.placeholder boundingRectWithSize:rect.size options:0 attributes:attributes context:nil]; 
        [self.placeholder drawAtPoint:CGPointMake(0, (rect.size.height/2)-boundingRect.size.height/2) withAttributes:attributes]; } 
    else { // iOS 6 
        [colour setFill]; 
        [self.placeholder drawInRect:rect withFont:self.font lineBreakMode:NSLineBreakByTruncatingTail alignment:self.textAlignment]; 
    } 
 } 

Credit: http://www.brightec.co.uk/blog/how-change-colour-uitextfields-placeholder-text-ios7-and-still-support-ios6

like image 81
Pablo Romeu Avatar answered Nov 18 '22 10:11

Pablo Romeu


Overriding the drawPlaceholderInRect: method to draw our own placeholder text.

- (void)drawPlaceholderInRect:(CGRect)rect
{
    UIColor *colour = [UIColor darkColor];
    
    if ([self.placeholder respondsToSelector:@selector(drawInRect:withAttributes:)]) {
        // iOS7 and later
        NSDictionary *attributes = @{NSForegroundColorAttributeName: colour, NSFontAttributeName: self.font};
        CGRect boundingRect = [self.placeholder boundingRectWithSize:rect.size options:0 attributes:attributes context:nil];
        [self.placeholder drawAtPoint:CGPointMake(0, (rect.size.height/2)-boundingRect.size.height/2) withAttributes:attributes];
    }
    else {
        // iOS 6
        [colour setFill];
        [self.placeholder drawInRect:rect withFont:self.font lineBreakMode:NSLineBreakByTruncatingTail alignment:self.textAlignment];
    }
}

OR

This case, likely crash if the internal variable changes in the future

Programmatically:

  [self.MyTextField setValue:[UIColor blackColor] forKeyPath:@"_placeholderLabel.textColor"];

In UIStoryBoard:

enter image description here

OR

EDIT: Playing with UITextFiled delegates. its like a tricky:

-(void)viewDidLoad{
  self.mytxtField.text = @"PlaceholderText";
  self.mytxtField.delegate = self;
   self.mytxtField.textColor = [UIColor darkGrayColor];

}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
           if ([self.mytxtField.text isEqualToString: @"PlaceholderText"]) {
             self.mytxtField.text = @"";
             self.mytxtField.textColor = [UIColor blackColor]; 
        }
    }


-(void)textFieldDidEndEditing:(UITextField *)textField{
      if ([self.mytxtField.text length]<1) {
            self.mytxtField.text =@"PlaceholderText";
            self.mytxtField.textColor = [UIColor darkGrayColor]; 

        }  
like image 34
Kumar KL Avatar answered Nov 18 '22 09:11

Kumar KL