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

前端 未结 5 953
没有蜡笔的小新
没有蜡笔的小新 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: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]];
    }
    

提交回复
热议问题