如何使用swift 2.0更改UITextfield占位符的颜色和字体大小?

如何在SWIFT 2.0中更改UITextfield placeholderfontsize

PlaceHolder TextFiled Swift 3.0

#1。 以编程方式设置占位符textfield颜色

  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 TextFiled:

在这里输入图像说明

– – – – – – – – – – – – – – – – 要么 – – – – – – – – – ——————–

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

  1. 设置文本字段placeHolder文本Enter Title

    在这里输入图像说明

  2. 点击textfield属性的身份检查器。

    在这里输入图像说明

  3. 用户定义运行时属性,添加颜色属性

    关键path: _placeholderLabel.textColor

    types: 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]) 

你可以试试这个示例代码

 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)