Change the font of the UITextField font

I change the color of the placeholder text 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}]; 
+42
ios objective-c uitextfield
Aug 15 '13 at 1:06 on
source share
9 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 parentheses 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) 
+113
Aug 15 '13 at 1:34 on
source share

For the convenience of fast people:

 someTextField.attributedPlaceholder = NSAttributedString(string: "someString", attributes:[NSForegroundColorAttributeName: UIColor.lightGrayColor(), NSFontAttributeName: PlaceHolderFont]) 
+8
Sep 30 '14 at 21:18
source share

for Swift users:

 let font = UIFont(name: "Avenir", size: 16)! let attributes = [ NSForegroundColorAttributeName: UIColor.lightGrayColor(), NSFontAttributeName : font] textField.attributedPlaceholder = NSAttributedString(string: "Type something", attributes:attributes) 

Make sure you spell the font name correctly.

+8
Apr 16 '15 at 16:09
source share

You must subclass UITextField and override the method:

 - (void)drawPlaceholderInRect:(CGRect)rect; 

The following is the implementation:

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

You can find more information here.

+6
Jul 29 '15 at 5:43
source share

Check out @ddevaz's answer.

Subclass UITextField Solution

The answer works fine for UITextField . But when I use a subclass of UITextField and try to execute this method in it. Then it does not work.

I found another solution only when subclassing UITextField . Override the method below in a subclass of UITextField and it will do the 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]; } 
+2
Aug 26 '14 at 5:51 on
source share

If you want to support both iOS 6 and previous versions, follow these steps:

 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"]; } 
+1
Oct. 15 '14 at 12:12
source share

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) 
+1
Dec 20 '17 at 17:39 on
source share

You can use the code below First find the name of the font accepted in the system for your custom font

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

and then use the code below, make your placeholder with custom font

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

Otherwise, you may be able to get an object nil [1] for a font that is not found

0
Oct. 28 '15 at 9:51 on
source share
  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 the UIFont method (name: "Menlo", size: 17.0), if the font name cannot get it in ios, then it will be null, so we need to provide a default font. This has been explained above.

You can use the code below. First, find the name of the system-accepted font for your custom font.

 for (NSString *familyName in [UIFont familyNames]) { for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) { NSLog(@"%@", fontName); } } 
0
Jul 14 '17 at 1:31 on
source share



All Articles