如何使用swift 2.0更改UITextfield占位符颜色和fontsize?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用swift 2.0更改UITextfield占位符颜色和fontsize?相关的知识,希望对你有一定的参考价值。
如何在SWIFT 2.0中更改UITextfield
placeholder
和fontsize
?
答案
#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
要么
txtTitle.attributedPlaceholder = NSAttributedString(string:"Enter Title", attributes: [NSForegroundColorAttributeName: yellowColor])
注意:Name
是textField
的占位符。
PlaceHolder文本字段:
- - - - - - - - - - - - - - - - 要么 - - - - - - - - - --------------------
#2。在运行时属性设置占位符文本字段颜色
- 设置textfield placeHolder文本
Enter Title
- 单击textfield属性的标识检查器。
- 用户定义运行时属性,添加颜色属性
关键路径:
_placeholderLabel.textColor
类型:Color
价值:Your Color or RGB value
PlaceHolder TextFiled:
另一答案
针对Swift 3进行了更新
如果要更改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])
另一答案
对于swift 4而不是
NSForegroundColorAttributeName
使用
NSAttributedStringKey.foregroundColor
另一答案
您可以尝试使用此示例代码
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)
另一答案
文本域的占位符目标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;
另一答案
对于swift 4.2而不是
yourTextFieldName.attributedPlaceholder = NSAttributedString(string:
"placeholder text", attributes: [NSForegroundColorAttributeName: UIColor.white])
使用
yourTextFieldName.attributedPlaceholder = NSAttributedString(string:"placeholder tex",
attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])
另一答案
设置Textfield占位符
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)
另一答案
使用UITextField的子类很容易。
添加placeholderColor
属性以轻松设置颜色,然后观察者更改.placeholder
以应用颜色(使用.attributedPlaceholder
属性)
var placeholderColor: UIColor = .lightGray
override var placeholder: String? {
didSet {
let attributes = [ NSAttributedString.Key.foregroundColor: placeholderColor ]
attributedPlaceholder = NSAttributedString(string: placeholder ?? "", attributes: attributes)
}
}
您需要以编程方式设置占位符文本以应用颜色。
以上是关于如何使用swift 2.0更改UITextfield占位符颜色和fontsize?的主要内容,如果未能解决你的问题,请参考以下文章