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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:08:34  来源:igfitidea点击:

How to change UITextfield placeholder color and fontsize using swift 2.0?

iosswift2xcode7

提问by Xcode

How to change UITextfieldplaceholder& fontsizein SWIFT 2.0?

如何在 SWIFT 2.0 中更改UITextfieldplaceholder& 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 : Nameis your placeholder of textField.

注意:Name是您的textField.

PlaceHolder TextFiled :

占位符文本文件:

enter image description here

在此处输入图片说明

-------------------------------- OR -------------------------------------

- - - - - - - - - - - - - - - - 或者 - - - - - - - - - --------------------

#2. set Placeholder textfield color at runtime attribute

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

  1. Set textfield placeHolder text Enter Title

    enter image description here

  2. Click on identity inspector of textfield property.

    enter image description here

  3. User Define Runtime Attributes, add color attributes

    Key Path : _placeholderLabel.textColor

    Type : Color

    value : Your Color or RGB value

    enter image description here

    PlaceHolder TextFiled :

    enter image description here

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

    在此处输入图片说明

  2. 单击文本字段属性的身份检查器。

    在此处输入图片说明

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

    关键路径: _placeholderLabel.textColor

    类型 : Color

    价值 : Your Color or RGB value

    在此处输入图片说明

    占位符文本文件:

    在此处输入图片说明

回答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 placeholderColorproperty to easily set the color, and then observer changing of .placeholderto apply the color to it (with use of .attributedPlaceholderproperty)

添加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 placeholderproperty in an UITextFieldextension. It will update color of placeholder whole project. You don't need to update your code in many places.

一个简单的解决方案是placeholderUITextField扩展中覆盖属性。它将更新占位符整个项目的颜色。您无需在许多地方更新您的代码。

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)