ios 更改 UITextField 占位符字体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18244790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing UITextField Placeholder font
提问by Bob Yousuk
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"
我正在使用以下代码更改占位符文本颜色,但是当我尝试添加 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"}];
This Works Fine:
这工作正常:
UIColor *color = [UIColor blackColor];
_nameField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Your Name" attributes:@{NSForegroundColorAttributeName: color}];
回答by ddevaz
Objective-C:
目标-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.)
(文字dictionary
键值对之间没有括号。)
Swift:
迅速:
let attributes = [
NSForegroundColorAttributeName: UIColor.blackColor(),
NSFontAttributeName : UIFont(name: "Roboto-Bold", size: 17)! // Note the !
]
someUITextField.attributedPlaceholder = NSAttributedString(string: "Placeholder Text", attributes:attributes)
回答by ayalcinkaya
Update for Swift 4.x:
Swift 4.x 的更新:
textField.attributedPlaceholder = NSAttributedString(string: "Placeholder Text", attributes: [
.foregroundColor: UIColor.lightGray,
.font: UIFont.boldSystemFont(ofSize: 14.0)
])
回答by harthoo
For the convenience of swift people:
为了方便快捷的人:
someTextField.attributedPlaceholder = NSAttributedString(string: "someString",
attributes:[NSForegroundColorAttributeName: UIColor.lightGrayColor(), NSFontAttributeName: PlaceHolderFont])
回答by tianglin
You should subclass UITextField, and override the method of:
您应该继承 UITextField,并覆盖以下方法:
- (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
更多你可以找到点击这里
回答by Kampai
Appreciate @ddevaz answer.
感谢@ddevaz 的回答。
UITextField Subclass solution
UITextField 子类解决方案
Above answer works perfect for UITextField
. But when i use UITextField
subclass and try to execute this method in it. Then its not working.
以上答案适用于UITextField
. 但是当我使用UITextField
子类并尝试在其中执行此方法时。然后它不工作。
I found other solution only when you subclass UITextField
. Override below method in your UITextField
subclass and it will do job for you.
仅当您将UITextField
. 在您的UITextField
子类中覆盖以下方法,它将为您完成工作。
- (void) drawPlaceholderInRect:(CGRect)rect {
NSDictionary *attrDictionary = @{
NSForegroundColorAttributeName: [UIColor lightGrayColor],
NSFontAttributeName : [UIFont fontWithName:@"Menlo" size:17.0]
};
[[self placeholder] drawInRect:rect withAttributes:attrDictionary];
}
回答by swift2geek
Working version for Swift 4
Swift 4 的工作版本
let attributes = [NSAttributedStringKey.foregroundColor: UIColor.black,
.font : UIFont.systemFont(ofSize: 14, weight: .regular)]
someTextfield.attributedPlaceholder = NSAttributedString(string: "Placeholder text", attributes:attributes)
回答by Christopher Larsen
Update for Swift 4
Swift 4 更新
let attributes = [
NSAttributedStringKey.foregroundColor: .black,
NSAttributedStringKey.font : UIFont(name: "Your font name", size: 14)!
]
someTextfield.attributedPlaceholder = NSAttributedString(string: "Placeholder text", attributes:attributes)
回答by Laszlo
If you want to support both iOS 6 and previous versions then:
如果你想同时支持 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"];
}
回答by Kirtikumar A.
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
否则有可能为找不到字体而获得 nil object[1]
回答by Longshihua
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.
注意:当我们使用 UIFont(name: "Menlo", size: 17.0) 方法时,如果字体名称在 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);
}
}