更改UITextField占位符字体

我正在改变占位符的文字颜色与下面的代码,但是当我尝试添加NSFontAttribute我得到编译器错误"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"}]; 

这工作正常:

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

Objective-C的:

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

(在dictionary键值对之间没有括号。)

迅速:

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

为了方便快捷的人:

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

对于Swift用户:

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

确保你写的字体名称正确。

你应该子类UITextField,并覆盖的方法:

 - (void)drawPlaceholderInRect:(CGRect)rect; 

下面是实现:

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

欲了解更多,请点击这里

感谢@ddevaz的答案。

UITextField子类解决scheme

以上答案适用于UITextField但是,当我使用UITextField子类,并尝试在其中执行此方法。 那么它不工作。

只有当你的子类UITextField时,我发现其他的解决scheme。 在你的UITextField子类中覆盖下面的方法,它会为你做的工作。

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

如果你想支持iOS 6和以前的版本,那么:

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

您可以使用下面的代码首先find您的自定义字体的系统接受字体的名称

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

然后使用下面的代码使您的占位符与自定义字体

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

否则,可能会有机会获取nil对象[1]找不到字体

  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) 

注意:当我们使用UIFont(name:“Menlo”,size:17.0)方法时,如果字体名不能在ios中获得,那么它将是零,所以我们需要提供默认的字体。 上面已经解释过了。

您可以使用下面的代码首先find您的自定义字体的系统接受字体的名称

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