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

前端 未结 10 648
小蘑菇
小蘑菇 2020-12-28 16:22

How to change UITextfield placeholder & fontsize in SWIFT 2.0?

相关标签:
10条回答
  • 2020-12-28 16:35

    open your identity inspector by selecting text field and then put " placeholderLabel.textColor " in key path by pressing + button . Give the type " Color " and in value select desired RGB color.

    0 讨论(0)
  • 2020-12-28 16:44

    Updated for Swift 3

    If you want to change the UITextField Placeholder color for Swift 3, use the following lines of code:

    let yourTextFieldName = UITextField(frame: CGRect(x: 0, y: 0, width: 180, height: 21))
    yourTextFieldName.attributedPlaceholder = NSAttributedString(string: "placeholder text", attributes: [NSForegroundColorAttributeName: UIColor.white])
    
    0 讨论(0)
  • 2020-12-28 16:44

    For swift 5.0 use NSAttributedString.Key.foregroundColor instead of NSForegroundColorAttributeName

    So, do it like so

    textField.attributedPlaceholder = NSAttributedString(string: "placeholder text", attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])
    
    0 讨论(0)
  • 2020-12-28 16:44

    Placeholder for textfield 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;
    
    0 讨论(0)
  • 2020-12-28 16:46

    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)
    
    0 讨论(0)
  • 2020-12-28 16:49

    A simple solution is override placeholder property in an UITextField extension. It will update color of placeholder whole project. You don't need to update your code in many places.

    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
          }
      }
    }
    
    
    0 讨论(0)
提交回复
热议问题