62

我正在使用以下代码更改占位符文本颜色,但是当我尝试添加 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}]; 
4

14 回答 14

143

目标-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)
于 2013-08-15T01:34:37.643 回答
25

Swift 4.x更新:

textField.attributedPlaceholder = NSAttributedString(string: "Placeholder Text", attributes: [
    .foregroundColor: UIColor.lightGray,
    .font: UIFont.boldSystemFont(ofSize: 14.0)
])
于 2015-04-16T16:09:53.947 回答
12

为了方便快捷的人:

someTextField.attributedPlaceholder = NSAttributedString(string: "someString", 
attributes:[NSForegroundColorAttributeName: UIColor.lightGrayColor(), NSFontAttributeName: PlaceHolderFont])
于 2014-09-30T21:18:27.360 回答
6

你应该继承UITextField,并重写方法:</p>

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

有关更多信息,请单击此处

于 2015-07-29T05:43:14.380 回答
5

感谢@ddevaz 的回答。

UITextField 子类解决方案

以上答案非常适合UITextField但是当我使用UITextField子类并尝试在其中执行此方法时。然后它不工作。

仅当您子类化时,我才找到其他解决方案UITextField。在您的子类中覆盖以下方法,UITextField它将为您完成工作。

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

    [[self placeholder] drawInRect:rect withAttributes:attrDictionary];
}
于 2014-08-26T05:51:51.493 回答
4

Swift 4 的工作版本

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

someTextfield.attributedPlaceholder = NSAttributedString(string: "Placeholder text", attributes:attributes)
于 2018-04-28T08:08:40.047 回答
3

Swift 4 的更新

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

someTextfield.attributedPlaceholder = NSAttributedString(string: "Placeholder text", attributes:attributes)
于 2017-12-20T17:39:48.217 回答
2

Swift 4.2 + 更新

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

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

更多属性键的参考 - NSAttributedString.Key

于 2020-05-31T11:07:01.963 回答
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"];
}
于 2014-10-15T12:12:40.700 回答
0

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

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 object[1] for font not found

于 2015-10-28T09:51:16.883 回答
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)

注意:当我们使用 UIFont(name: "Menlo", size: 17.0) 方法时,如果字体名称在 ios 中无法获取,则为 nil,所以我们需要提供默认字体。上面已经解释过了。

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

for (NSString *familyName in [UIFont familyNames]) {
    for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
    NSLog(@"%@", fontName);
   }
 }
于 2017-07-14T01:31:57.617 回答
0

如果你有和我一样的问题。我通过 set Font 解决,因为在 stroryboard 中设置 Font 或在self.emailTextField.placeholder.

self.emailTextField.font = UIFont(..custom you font....)
于 2020-04-24T03:04:09.600 回答
0

Swift 4:UITextField使用子类更改占位符颜色

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

 }
于 2018-09-29T15:33:05.930 回答
0

您只需执行此操作即可更改文本和占位符的字体大小。

field.font = .systemFont(ofSize: 16)
于 2020-07-11T22:32:28.933 回答