Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing UITextField Placeholder font

I'm changing the placeholder text color with the following code, but when I try to add NSFontAttribute I get the compiler error "too many arguments to method call, expect 2 have 3"

 UIColor *color = [UIColor blackColor];
        _nameField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Your Name" attributes:@{NSForegroundColorAttributeName: color},@{NSFontAttributeName:@"Roboto-Bold"}]; 

This Works Fine:

 UIColor *color = [UIColor blackColor];
        _nameField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Your Name" attributes:@{NSForegroundColorAttributeName: color}]; 
like image 616
Bob Yousuk Avatar asked Aug 15 '13 01:08

Bob Yousuk


People also ask

Can you change placeholder font?

<input type="text" placeholder="A red placeholder text..">

Can we change placeholder font size?

Placeholder text will automatically inherit the font family and font size of the regular input text, but you may be in a situation where you want to change the placeholder text color.

How do I change placeholder color in swift4?

If Are you set from storyboard. extension UITextField{ @IBInspectable var placeHolderColor: UIColor? { get { return self. placeHolderColor } set { self. attributedPlaceholder = NSAttributedString(string:self.

How do I change the placeholder color on a storyboard?

The easiest method to modify the placeholder text color is through the Xcode storyboard interface builder. Select the UITextField of interest and open the identity inspector on the right. Click on the plus symbol in the User Defined Runtime Attributes and add a new row with Key Path as placeholderLabel.


3 Answers

Objective-C:

UIColor *color = [UIColor blackColor];    
someUITextField.attributedPlaceholder =
  [[NSAttributedString alloc] initWithString:@"Placeholder Text"
    attributes:@{
       NSForegroundColorAttributeName: color,
       NSFontAttributeName : [UIFont fontWithName:@"Roboto-Bold" size:17.0]
    }
  ];

(There are no brackets between literal dictionary key-value pairs.)

Swift:

let attributes = [
    NSForegroundColorAttributeName: UIColor.blackColor(),
    NSFontAttributeName : UIFont(name: "Roboto-Bold", size: 17)! // Note the !
]

someUITextField.attributedPlaceholder = NSAttributedString(string: "Placeholder Text", attributes:attributes)
like image 140
ddevaz Avatar answered Oct 23 '22 05:10

ddevaz


For the convenience of swift people:

someTextField.attributedPlaceholder = NSAttributedString(string: "someString", 
attributes:[NSForegroundColorAttributeName: UIColor.lightGrayColor(), NSFontAttributeName: PlaceHolderFont])
like image 25
harthoo Avatar answered Oct 23 '22 05:10

harthoo


Update for Swift 4.x:

textField.attributedPlaceholder = NSAttributedString(string: "Placeholder Text", attributes: [
    .foregroundColor: UIColor.lightGray,
    .font: UIFont.boldSystemFont(ofSize: 14.0)
])
like image 29
ayalcinkaya Avatar answered Oct 23 '22 04:10

ayalcinkaya