Bob Yousuk
Bob Yousuk

Reputation: 1175

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

Upvotes: 68

Views: 80482

Answers (14)

dave o grady
dave o grady

Reputation: 822

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

field.font = .systemFont(ofSize: 16)

Upvotes: 0

UditS
UditS

Reputation: 1956

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

Upvotes: 3

Papon Smc
Papon Smc

Reputation: 688

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....)

Upvotes: 0

McDonal_11
McDonal_11

Reputation: 4075

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)
        }

 }

Upvotes: 0

ayalcinkaya
ayalcinkaya

Reputation: 3343

Update for Swift 4.x:

textField.attributedPlaceholder = NSAttributedString(string: "Placeholder Text", attributes: [
    .foregroundColor: UIColor.lightGray,
    .font: UIFont.boldSystemFont(ofSize: 14.0)
])

Upvotes: 31

swift2geek
swift2geek

Reputation: 1812

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)

Upvotes: 4

Christopher Larsen
Christopher Larsen

Reputation: 1411

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)

Upvotes: 3

tianglin
tianglin

Reputation: 226

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

Upvotes: 7

Longshihua
Longshihua

Reputation: 425

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

Upvotes: 0

Kirtikumar A.
Kirtikumar A.

Reputation: 4204

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

Upvotes: 0

ddevaz
ddevaz

Reputation: 2263

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)

Upvotes: 154

Laszlo
Laszlo

Reputation: 2778

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

Upvotes: 1

Mehul Patel
Mehul Patel

Reputation: 23053

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

Upvotes: 5

harthoo
harthoo

Reputation: 813

For the convenience of swift people:

someTextField.attributedPlaceholder = NSAttributedString(string: "someString", 
attributes:[NSForegroundColorAttributeName: UIColor.lightGrayColor(), NSFontAttributeName: PlaceHolderFont])

Upvotes: 12

Related Questions