67

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}]; 
Ajumal
  • 1,048
  • 11
  • 33
Bob Yousuk
  • 1,155
  • 3
  • 10
  • 16

14 Answers14

153

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)
ddevaz
  • 2,253
  • 1
  • 17
  • 10
  • 4
    NSFontAttributeName expects a UIFont object, not a string. You would need to use something like NSFontAttributeName : [UIFont fontWithName:@"Roboto-Bold" size:17.0] – jmstone617 Jan 24 '14 at 18:11
  • 3
    This changed the color for me but not the font. – kraftydevil Sep 23 '14 at 08:06
  • Yeah, in iOS7 the font is not changed. – xi.lin Jun 23 '15 at 09:58
  • When I use this code to change the font size, the positioning of the placeholder within the textfield moves down so it is no longer centered vertically. Any ideas on how to increase the font size of the placeholder text but keep it centered within the textfield? – RanLearns May 27 '17 at 00:54
30

Update for Swift 4.x:

textField.attributedPlaceholder = NSAttributedString(string: "Placeholder Text", attributes: [
    .foregroundColor: UIColor.lightGray,
    .font: UIFont.boldSystemFont(ofSize: 14.0)
])
AamirR
  • 11,672
  • 4
  • 59
  • 73
ayalcinkaya
  • 3,303
  • 29
  • 25
  • It works well on Xcode 8.3.2 & Swift 3, just a casting of attributes using `as [String : Any]` is required – Claus Jun 07 '17 at 16:07
12

For the convenience of swift people:

someTextField.attributedPlaceholder = NSAttributedString(string: "someString", 
attributes:[NSForegroundColorAttributeName: UIColor.lightGrayColor(), NSFontAttributeName: PlaceHolderFont])
harthoo
  • 813
  • 10
  • 11
7

You should subclass UITextField, and override the method of:

- (void)drawPlaceholderInRect:(CGRect)rect;

Here is the implementation below:

- (void)drawPlaceholderInRect:(CGRect)rect {
     NSDictionary *attributes = @{
                             NSForegroundColorAttributeName : [UIColor lightGrayColor],
                             NSFontAttributeName : [UIFont italicSystemFontOfSize:self.font.pointSize]
                             };
    // center vertically
    CGSize textSize = [self.placeholder sizeWithAttributes:attributes];
    CGFloat hdif = rect.size.height - textSize.height;
    hdif = MAX(0, hdif);
    rect.origin.y += ceil(hdif/2.0);
    [[self placeholder] drawInRect:rect withAttributes:attributes];
}

For more you can find click here

toddg
  • 2,863
  • 2
  • 18
  • 33
tianglin
  • 226
  • 3
  • 14
5

Appreciate @ddevaz answer.

UITextField Subclass solution

Above answer works perfect for UITextField. But when i use UITextField subclass and try to execute this method in it. Then its not working.

I found other solution only when you subclass UITextField. Override below method in your UITextField subclass and it will do job for you.

- (void) drawPlaceholderInRect:(CGRect)rect {
    NSDictionary *attrDictionary = @{
                                     NSForegroundColorAttributeName: [UIColor lightGrayColor],
                                     NSFontAttributeName : [UIFont fontWithName:@"Menlo" size:17.0]
                                     };

    [[self placeholder] drawInRect:rect withAttributes:attrDictionary];
}
Kampai
  • 22,848
  • 21
  • 95
  • 95
4

Working version for Swift 4

let attributes = [NSAttributedStringKey.foregroundColor: UIColor.black,
                      .font : UIFont.systemFont(ofSize: 14, weight: .regular)]

someTextfield.attributedPlaceholder = NSAttributedString(string: "Placeholder text", attributes:attributes)
swift2geek
  • 1,697
  • 1
  • 20
  • 27
3

Update for Swift 4

let attributes = [
    NSAttributedStringKey.foregroundColor: .black,
    NSAttributedStringKey.font : UIFont(name: "Your font name", size: 14)!
]

someTextfield.attributedPlaceholder = NSAttributedString(string: "Placeholder text", attributes:attributes)
Christopher Larsen
  • 1,375
  • 15
  • 22
3

Update for Swift 4.2 +

let attributes = [NSAttributedString.Key.foregroundColor: UIColor.black,
                  .font: UIFont.systemFont(ofSize: 14)]

searchTextField.attributedPlaceholder = NSAttributedString(string: "Placeholder text",
                                                           attributes: attributes)

Reference for more attribute Keys - NSAttributedString.Key

UditS
  • 1,936
  • 17
  • 37
1

If you want to support both iOS 6 and previous versions then:

UIColor *placeholderColor = [UIColor lightTextColor];
UIFont *placeholderFont = [UIFont systemFontOfSize:[UIFont systemFontSize]];

if ([textField respondsToSelector:@selector(attributedPlaceholder)]) {
#ifdef __IPHONE_6_0
    textField.attributedPlaceholder = [[NSAttributedString alloc]
        initWithString:textField.placeholder attributes: // NOTE: textField.placeholder can't be nil
              @{ NSForegroundColorAttributeName : placeholderColor,
                 NSFontAttributeName : placeholderFont }];
#endif
} else { // for pre-iOS6
    [textField setValue:placeholderColor forKeyPath:@"_placeholderLabel.textColor"];
    [textField setValue:placeholderFont forKeyPath:@"_placeholderLabel.font"];
}
Laszlo
  • 2,803
  • 2
  • 28
  • 33
0

You can use below code First find name of system accepted font for your Custom font

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

and then use below code make your placeholder with custom font

searchTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Where are you shopping?" attributes:@{NSFontAttributeName : font }];

Else there can be chance to get nil object[1] for font not found

AlexanderZ
  • 2,128
  • 3
  • 20
  • 24
Kirtikumar A.
  • 4,140
  • 43
  • 43
0
    let attributes: [NSAttributedStringKey : Any] = [NSAttributedStringKey.foregroundColor: UIColor.black,
 NSAttributedStringKey.font: UIFont(name: "Menlo", size: 17.0) ?? UIFont.systemFont(ofSize: 17.0) ]


    textField.attributedPlaceholder = NSAttributedString(string: "Placeholder Text", attributes: attributes)

Note: when we use UIFont(name: "Menlo", size: 17.0) method, if the font name can't get it in ios , so it will be nil, so we need to provide the default font. It was explained above.

You can use below code First find name of system accepted font for your Custom font

for (NSString *familyName in [UIFont familyNames]) {
    for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
    NSLog(@"%@", fontName);
   }
 }
Longshihua
  • 395
  • 1
  • 3
  • 20
0

Swift 4: Placeholder Color change using UITextField subclass

override func drawPlaceholder(in rect: CGRect) {

        let color = UIColor(red: 210/255, green: 210/255, blue: 210/255, alpha: 1.0)

        if (placeholder?.responds(to: #selector(NSString.draw(in:withAttributes:))))! {

            let fontS = UIFont.systemFont(ofSize: 12)

            let attributes = [NSAttributedStringKey.foregroundColor: color, NSAttributedStringKey.font: fontS] as [NSAttributedStringKey : Any]

            let boundingRect: CGRect = placeholder!.boundingRect(with: rect.size, options: [], attributes: attributes, context: nil)

            placeholder?.draw(at: CGPoint(x: 0, y: (rect.size.height / 2) - boundingRect.size.height / 2), withAttributes: attributes)
        }

 }
McDonal_11
  • 3,935
  • 6
  • 24
  • 55
0

if you have problem same me. I solve by set Font because some time font can't change when set Font in stroryboard or set in self.emailTextField.placeholder .

self.emailTextField.font = UIFont(..custom you font....)
Papon Smc
  • 576
  • 4
  • 11
0

You can change the font size of the text and placeholder by simply doing this.

field.font = .systemFont(ofSize: 16)
dave o grady
  • 788
  • 8
  • 12