如何在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
是你的文本框的占位符。
占位符文本框:
-------------------------------- 或 -------------------------------------
#2. 在运行时属性中设置占位符文本颜色
已更新至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 5
在Swift 5.0中,请使用NSAttributedString.Key.foregroundColor,而不是NSForegroundColorAttributeName
因此,请像这样操作
textField.attributedPlaceholder = NSAttributedString(string: "placeholder text", attributes: [NSAttributedString.Key.foregroundColor: 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)
文本框的占位符 Objective 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;
使用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)
}
}
您需要通过编程设置占位文本,才能应用颜色。
设置文本框占位符
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)
通过选择文本字段打开您的身份检查器,然后按下 + 按钮,在关键路径中输入“placeholderLabel.textColor”。 将类型设置为“Color”,在值中选择所需的RGB颜色。
Swift 5
textfiled.attributedPlaceholder = NSAttributedString(string:NSLocalizedString("Input Group Name", comment: "Input Group Name"), attributes: [NSAttributedString.Key.foregroundColor: yourColor.withAlphaComponent(0.5)])