如何使用swift 2.0更改UITextfield占位符颜色和fontsize?

问题描述 投票:14回答:8

如何在SWIFT 2.0中更改UITextfield placeholderfontsize

ios swift2 xcode7
8个回答
62
投票

#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])

注意:NametextField的占位符。

PlaceHolder文本字段:

enter image description here

- - - - - - - - - - - - - - - - 要么 - - - - - - - - - --------------------

#2。在运行时属性设置占位符文本字段颜色

  1. 设置textfield placeHolder文本Enter Title enter image description here
  2. 单击textfield属性的标识检查器。 enter image description here
  3. 用户定义运行时属性,添加颜色属性 关键路径:_placeholderLabel.textColor 类型:Color 价值:Your Color or RGB value enter image description here PlaceHolder TextFiled: enter image description here

10
投票

针对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])

9
投票

对于swift 4而不是

NSForegroundColorAttributeName

使用

NSAttributedStringKey.foregroundColor


6
投票

您可以尝试使用此示例代码

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)

1
投票

文本域的占位符目标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;

1
投票

对于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])

0
投票

设置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)

0
投票

使用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)
    }
}

您需要以编程方式设置占位符文本以应用颜色。

© www.soinside.com 2019 - 2024. All rights reserved.