ios 如何使用 swift 2.0 更改 UITextfield 占位符颜色和字体大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33414757/
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
How to change UITextfield placeholder color and fontsize using swift 2.0?
提问by Xcode
How to change UITextfield
placeholder
& fontsize
in SWIFT 2.0?
如何在 SWIFT 2.0 中更改UITextfield
placeholder
& fontsize
?
回答by Kirit Modi
#1. set Placeholder textfield color Programmatically
#1. 以编程方式设置占位符文本字段颜色
var myMutableStringTitle = NSMutableAttributedString()
let Name = "Enter Title" // PlaceHolderText
myMutableStringTitle = NSMutableAttributedString(string:Name, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 20.0)!]) // Font
myMutableStringTitle.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range:NSRange(location:0,length:Name.characters.count)) // Color
txtTitle.attributedPlaceholder = myMutableStringTitle
OR
或者
txtTitle.attributedPlaceholder = NSAttributedString(string:"Enter Title", attributes: [NSForegroundColorAttributeName: yellowColor])
Note : Name
is your placeholder of textField
.
注意:Name
是您的textField
.
PlaceHolder TextFiled :
占位符文本文件:
-------------------------------- OR -------------------------------------
- - - - - - - - - - - - - - - - 或者 - - - - - - - - - --------------------
#2. set Placeholder textfield color at runtime attribute
#2. 在运行时属性设置占位符文本字段颜色
Set textfield placeHolder text
Enter Title
Click on identity inspector of textfield property.
User Define Runtime Attributes, add color attributes
Key Path :
_placeholderLabel.textColor
Type :
Color
value :
Your Color or RGB value
PlaceHolder TextFiled :
回答by Kiran jadhav
Updated for Swift 3
为 Swift 3 更新
If you want to change the UITextField Placeholder color for Swift 3, use the following lines of code:
如果要更改 Swift 3 的 UITextField 占位符颜色,请使用以下代码行:
let yourTextFieldName = UITextField(frame: CGRect(x: 0, y: 0, width: 180, height: 21))
yourTextFieldName.attributedPlaceholder = NSAttributedString(string: "placeholder text", attributes: [NSForegroundColorAttributeName: UIColor.white])
回答by Serguei Vinnitskii
For swift 4 instead of
对于 swift 4 而不是
NSForegroundColorAttributeName
NSForegroundColorAttributeName
use
用
NSAttributedStringKey.foregroundColor
NSAttributedStringKey.foregroundColor
回答by Jin
For swift 5.0 use NSAttributedString.Key.foregroundColorinstead of NSForegroundColorAttributeName
对于 swift 5.0 使用NSAttributedString.Key.foregroundColor而不是NSForegroundColorAttributeName
So, do it like so
所以,这样做
textField.attributedPlaceholder = NSAttributedString(string: "placeholder text", attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])
回答by Jamil
You can try with this sample code
您可以尝试使用此示例代码
let textFld = UITextField();
textFld.frame = CGRectMake(0,0, 200, 30)
textFld.center = self.view.center;
textFld.attributedPlaceholder = NSAttributedString(string:"Test Data for place holder", attributes:[NSForegroundColorAttributeName: UIColor.blueColor(),NSFontAttributeName :UIFont(name: "Arial", size: 10)!])
self.view.addSubview(textFld)
回答by Dharmesh Mansata
Placeholder for textfield Objective C
文本字段目标 C 的占位符
NSString* str = @"Placeholder text...";
NSRange range1 = [str rangeOfString:@"Placeholder text..."];
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:str];
[attributedText setAttributes:@{
NSFontAttributeName:[UIFont fontWithName:customFont_NotoSans_Regular size:13.0]
}
range:range1];
[attributedText addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:range1];
txtFld.font = [UIFont fontWithName:customFont_NotoSans_Regular size:13.0];
txtFld.keyboardType = UIKeyboardTypeDefault;
txtFld.attributedPlaceholder = attributedText;
回答by Demosthese
It's easy to do with a subclass of UITextField.
使用 UITextField 的子类很容易做到。
Add placeholderColor
property to easily set the color, and then observer changing of .placeholder
to apply the color to it (with use of .attributedPlaceholder
property)
添加placeholderColor
属性以轻松设置颜色,然后观察者更改以.placeholder
对其应用颜色(使用.attributedPlaceholder
属性)
var placeholderColor: UIColor = .lightGray
override var placeholder: String? {
didSet {
let attributes = [ NSAttributedString.Key.foregroundColor: placeholderColor ]
attributedPlaceholder = NSAttributedString(string: placeholder ?? "", attributes: attributes)
}
}
You do need to set the placeholder text programatically for the color to apply.
您确实需要以编程方式设置占位符文本以应用颜色。
回答by huync
A simple solution is override placeholder
property in an UITextField
extension. It will update color of placeholder whole project. You don't need to update your code in many places.
一个简单的解决方案是placeholder
在UITextField
扩展中覆盖属性。它将更新占位符整个项目的颜色。您无需在许多地方更新您的代码。
extension UITextField {
var placeholder: String? {
get {
attributedPlaceholder?.string
}
set {
guard let newValue = newValue else {
attributedPlaceholder = nil
return
}
let attributes: [NSAttributedString.Key: Any] = [.foregroundColor: Color.textFieldPlaceholder.color]
let attributedText = NSAttributedString(string: newValue, attributes: attributes)
attributedPlaceholder = attributedText
}
}
}
回答by CSE 1994
set Textfield placeholder
设置文本字段占位符
let leftBarButtonItem = UIBarButtonItem.init(image: UIImage(named:"ic_nav-bar_back.png"), landscapeImagePhone: nil, style: .plain, target: viewController, action: #selector(viewController.buttonClick(_:)))
leftBarButtonItem.imageInsets = UIEdgeInsets(top: 0, left: -15, bottom: 0, right: 0)
leftBarButtonItem.tintColor = UIColor(hex: 0xED6E19)
viewController.navigationItem.setLeftBarButton(leftBarButtonItem, animated: true)