2

Can Change UITextField Placeholder Text Color as follows:

[txtField setValue:[UIColor colorWithRed:128.0/255.0 green:128.0/255.0 blue:128.0/255.0 alpha:1.0]
                    forKeyPath:@"_placeholderLabel.textColor"];

Similarly is there any way to Resize UITextField Placeholder Text Font to Fit UITextField width?

Note: I have text fields in custom cell of table view and also using autolayout in custom cell.

Ronak Chaniyara
  • 5,335
  • 3
  • 24
  • 51

4 Answers4

2

I met the same issue, here is the solution:

UILabel *placeHolderLabel = [self.registrationCodeTextfield valueForKey:@"_placeholderLabel"];
placeHolderLabel.adjustsFontSizeToFitWidth = YES;
Yuchao Zhou
  • 1,062
  • 14
  • 19
0

Here is one coding that i used in ViewDidLoad to set the placeholder of textfield of desirable size. -----------------------------------code-----------------------------------------

UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 12, 20)];
textfieldUsername.leftView = paddingView;
textfieldUsername.leftViewMode = UITextFieldViewModeAlways;

[[UITextField appearance] setTintColor:[UIColor whiteColor]];
[textfieldUsername setValue:[UIColor lightTextColor] forKeyPath:@"_placeholderLabel.textColor"];

(Nj)

Sabby
  • 403
  • 3
  • 15
  • 1
    not working, i want to scale font of textfield placeholder text and i have fixed width textfield which truncates placeholder text@Garry – Ronak Chaniyara Aug 08 '16 at 12:16
  • @Ronak Chaniyara Did manage to solve this issue? I have exactly same problem. Please let me know how you solved it!! Thanks – Mr.KLD Aug 18 '16 at 02:11
0

Here is code

 textField.placeholder = @"Pavankumar";
 [textField setValue:[UIColor greenColor]
                    forKeyPath:@"_placeholderLabel.textColor"];

see below code it change font of the placeholder

textField.attributedPlaceholder = [[NSAttributedString alloc]initWithString:@"Pavankumar" attributes:@{NSForegroundColorAttributeName:[UIColor greenColor],NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue-light" size:12.0]}];
Ronak Chaniyara
  • 5,335
  • 3
  • 24
  • 51
Pavankumar
  • 288
  • 1
  • 10
0

You have the placeholder text and you have the frame of the uitextfield . From these two you can calculate the font size which fits and assign as a Attributed Placeholder String.

If calculated font size is less than your limit , then use that font size other wise use the limit

vivek
  • 45
  • 1
  • 7