ios-更改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}]; 
Bob Yousuk asked 2020-01-13T21:45:16Z
11个解决方案
132 votes

目标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)
ddevaz answered 2020-01-13T21:45:34Z
21 votes

Swift 4.x更新:

textField.attributedPlaceholder = NSAttributedString(string: "Placeholder Text", attributes: [
    .foregroundColor: UIColor.lightGray,
    .font: UIFont.boldSystemFont(ofSize: 14.0)
])
ayalcinkaya answered 2020-01-13T21:45:54Z
8 votes

为了方便人们:

someTextField.attributedPlaceholder = NSAttributedString(string: "someString", 
attributes:[NSForegroundColorAttributeName: UIColor.lightGrayColor(), NSFontAttributeName: PlaceHolderFont])
harthoo answered 2020-01-13T21:46:14Z
6 votes

您应该继承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];
}

欲了解更多信息,请点击这里

tianglin answered 2020-01-13T21:46:42Z
4 votes

欣赏@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];
}
Kampai answered 2020-01-13T21:47:16Z
4 votes

Swift 4的工作版本

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

someTextfield.attributedPlaceholder = NSAttributedString(string: "Placeholder text", attributes:attributes)
swift2geek answered 2020-01-13T21:47:35Z
3 votes

Swift 4更新

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

someTextfield.attributedPlaceholder = NSAttributedString(string: "Placeholder text", attributes:attributes)
Christopher Larsen answered 2020-01-13T21:47:55Z
1 votes

如果要同时支持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"];
}
Laszlo answered 2020-01-13T21:48:15Z
0 votes

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

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]

Kirtikumar A. answered 2020-01-13T21:48:44Z
0 votes
    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);
   }
 }
Longshihua answered 2020-01-13T21:49:09Z
0 votes

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

 }
McDonal_11 answered 2020-01-13T21:49:28Z
translate from https://stackoverflow.com:/questions/18244790/changing-uitextfield-placeholder-font