How to set the color of the place holder text for a UITextField while preserving its existing properties?

前端 未结 5 875
没有蜡笔的小新
没有蜡笔的小新 2021-02-18 18:37

I have seen some answers that show how to change the placeHolder text color for UITextField by overriding the drawPlaceholderInRect: method such as this one:

iPhone UITe

相关标签:
5条回答
  • 2021-02-18 18:54

    here See my outputThis is just for get system font name

    for (NSString *familyName in [UIFont familyNames]) {
         for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
             NSLog(@"%@", fontName);
    

    And add a this code in your viewDidLoad

    _textFieldEmail.attributedPlaceholder =
    [[NSAttributedString alloc] initWithString:@"e-mail address"
                                    attributes:@{
                                                 NSFontAttributeName : [UIFont fontWithName:@"Roboto-LightItalic" size:17.0]
                                                 }
     ];
    
    _textFieldPassword.attributedPlaceholder =
    [[NSAttributedString alloc] initWithString:@"password"
                                    attributes:@{
                                                 NSFontAttributeName : [UIFont fontWithName:@"Roboto-LightItalic" size:17.0]
                                                 }
     ];
    
    0 讨论(0)
  • 2021-02-18 18:58

    There are multiple ways to achieve this.

    Using Interface Builder or Storyboard

    • Select the Textfield for which you want to change placeholder color
    • go to the Identity inspector menu on Top right of Xcode
    • Add the key value pair this way

    Key path = _placeholderLabel.textColor

    Click the Type and chose Color attribute .

    Then select the color in value.

    Set The placeholder color using code :

    Process 1:

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

    Process 2 :

    Override drawPlaceholderInRect:(CGRect)rect method

    - (void) drawPlaceholderInRect:(CGRect)rect {
        [[UIColor blueColor] setFill];
        [[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:14]];
    }
    
    0 讨论(0)
  • 2021-02-18 19:04

    From iOS 6,

    Without any subclassing, one can accomplish this with a couple lines of code like so:

    UIColor *color = [UIColor blackColor];
    textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholderText attributes:@{NSForegroundColorAttributeName: color}];
    
    0 讨论(0)
  • 2021-02-18 19:08

    There is indeed a much better way to handle this now. This will work for iOS 6 and 7.

    (Note this example, I created the code in AwakeFromNib since it won't be changing colors once set. But if you don't use XIB, you will have to change the location where you put this code, such as in drawPlaceholderInRect,)

    In this example, we create a subclass of UITextField, override awakeFromNib and then set the placeHolder text color to red:

    - (void)awakeFromNib
    {
        if ([self.attributedPlaceholder length])
        {
            // Extract attributes
            NSDictionary * attributes = (NSMutableDictionary *)[ (NSAttributedString *)self.attributedPlaceholder attributesAtIndex:0 effectiveRange:NULL];
    
            NSMutableDictionary * newAttributes = [[NSMutableDictionary alloc] initWithDictionary:attributes];
    
            [newAttributes setObject:[UIColor redColor] forKey:NSForegroundColorAttributeName];
    
            // Set new text with extracted attributes
            self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:[self.attributedPlaceholder string] attributes:newAttributes];
    
        }
    }
    

    The nice thing about this approach is that it maintains the current UITextField properties for the placeHolder string and so will allow you to work in IB for most of what you set. In addition, its much more efficient than doing everytime you need to draw. It also allows you to change any other property you want on the placeHolder text while maintaining the rest of the properties.

    As mentioned above, if don't use XIBs, then you will need to call this at some other time. If you do put this code in the drawPlaceholderInRect: method, then make sure you call [super drawPlaceholderInRect:] at the end of it.

    0 讨论(0)
  • 2021-02-18 19:11

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

    Source.

    0 讨论(0)
提交回复
热议问题