Changing UITextField Placeholder font
IosObjective CUitextfieldIos Problem Overview
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}];
Ios Solutions
Solution 1 - Ios
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)
Solution 2 - Ios
Update for Swift 4.x:
textField.attributedPlaceholder = NSAttributedString(string: "Placeholder Text", attributes: [
.foregroundColor: UIColor.lightGray,
.font: UIFont.boldSystemFont(ofSize: 14.0)
])
Solution 3 - Ios
For the convenience of swift people:
someTextField.attributedPlaceholder = NSAttributedString(string: "someString",
attributes:[NSForegroundColorAttributeName: UIColor.lightGrayColor(), NSFontAttributeName: PlaceHolderFont])
Solution 4 - Ios
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
Solution 5 - Ios
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];
}
Solution 6 - Ios
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)
Solution 7 - Ios
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)
Solution 8 - Ios
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
Solution 9 - Ios
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"];
}
Solution 10 - Ios
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
Solution 11 - Ios
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);
}
}
Solution 12 - Ios
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)
}
}
Solution 13 - Ios
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....)
Solution 14 - Ios
You can change the font size of the text and placeholder by simply doing this.
field.font = .systemFont(ofSize: 16)