Wednesday 4 June 2014

Change UITextField’s placeholder color without subclassing it

Reference URL :
1. http://stackoverflow.com/questions/1340224/iphone-uitextfield-change-placeholder-text-color

The safe way to customize UITextField’s placeholder is subclassing the UITextField and overriding placeholderRectForBounds:, Apple won’t bother you on this one. However, if you want to take the risk, you can try this way:
[self.MyTextField setValue:[UIColor darkGrayColor] forKeyPath:@"_placeholderLabel.textColor"];


if ([textField respondsToSelector:@selector(setAttributedPlaceholder:)]) {
  UIColor *color = [UIColor blackColor];
  textField.attributedPlaceholder = [[NSAttributedString alloc] 
         initWithString:placeholderText 
         attributes:@{NSForegroundColorAttributeName: color}];
} else {
  NSLog(@"Cannot set placeholder text's color, because 
            deployment target is earlier than iOS 6.0");
  // TODO: Add fall-back code to set placeholder color.
}

No comments:

Post a Comment