简体   繁体   中英

How to Resize UITextField Placeholder Text to Fit UITextField width?

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.

I met the same issue, here is the solution:

UILabel *placeHolderLabel = [self.registrationCodeTextfield valueForKey:@"_placeholderLabel"];
placeHolderLabel.adjustsFontSizeToFitWidth = YES;

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)

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM