我正在使用以下代码更改占位符文本颜色,但是当我尝试添加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}];
答案 0 :(得分:129)
<强>目标-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)
答案 1 :(得分:21)
更新 Swift 4.x :
textField.attributedPlaceholder = NSAttributedString(string: "Placeholder Text", attributes: [
.foregroundColor: UIColor.lightGray,
.font: UIFont.boldSystemFont(ofSize: 14.0)
])
答案 2 :(得分:8)
为了方便快捷的人:
someTextField.attributedPlaceholder = NSAttributedString(string: "someString",
attributes:[NSForegroundColorAttributeName: UIColor.lightGrayColor(), NSFontAttributeName: PlaceHolderFont])
答案 3 :(得分:6)
您应该继承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];
}
您可以找到更多click here
答案 4 :(得分:4)
欣赏@ddevaz答案。
以上回答适用于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];
}
答案 5 :(得分:4)
Swift 4的工作版
let attributes = [NSAttributedStringKey.foregroundColor: UIColor.black,
.font : UIFont.systemFont(ofSize: 14, weight: .regular)]
someTextfield.attributedPlaceholder = NSAttributedString(string: "Placeholder text", attributes:attributes)
答案 6 :(得分: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)
答案 7 :(得分:1)
如果您想同时支持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"];
}
答案 8 :(得分:1)
针对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
答案 9 :(得分: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对象[1]
答案 10 :(得分: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(名称:“Menlo”,大小:17.0)方法时,如果字体名称无法在ios中获取,那么它将为nil,因此我们需要提供默认字体。上面已经解释过了。
您可以使用以下代码首先查找自定义字体的系统接受字体名称
for (NSString *familyName in [UIFont familyNames]) {
for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
NSLog(@"%@", fontName);
}
}
答案 11 :(得分:0)
快速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)
}
}
答案 12 :(得分:0)
如果我也有问题。我通过设置字体解决,因为在stroryboard中设置字体或在self.emailTextField.placeholder
中设置字体时,某些时间的字体无法更改。
self.emailTextField.font = UIFont(..custom you font....)
答案 13 :(得分:0)
只需执行此操作,即可更改文本和占位符的字体大小。
field.font = .systemFont(ofSize: 16)