Changing Placeholder Text Color with Swift

前端 未结 30 2355
独厮守ぢ
独厮守ぢ 2020-11-29 15:57

I have a design that implements a dark blue UITextField, as the placeholder text is by default a dark grey colour I can barely make out what the place holder te

相关标签:
30条回答
  • 2020-11-29 16:10

    Just write below code into Appdelegate's didFinishLaunchingWithOptions method use this if you want to change in the whole app written in Swift 4.2

    UILabel.appearance(whenContainedInInstancesOf: [UITextField.self]).textColor = UIColor.white
    
    0 讨论(0)
  • 2020-11-29 16:11

    For Swift

    Create UITextField Extension

    extension UITextField{
    
        func setPlaceHolderColor(){
            self.attributedPlaceholder = NSAttributedString(string: self.placeholder!, attributes: [NSForegroundColorAttributeName : UIColor.white])
        }
    }
    

    If Are you set from storyboard.

    extension UITextField{
        @IBInspectable var placeHolderColor: UIColor? {
            get {
                return self.placeHolderColor
            }
            set {
                self.attributedPlaceholder = NSAttributedString(string:self.placeholder != nil ? self.placeholder! : "", attributes:[NSAttributedString.Key.foregroundColor : newValue!])
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-29 16:11

    Use like this in Swift,

     let placeHolderText = textField.placeholder ?? ""
     let str = NSAttributedString(string:placeHolderText!, attributes: [NSAttributedString.Key.foregroundColor :UIColor.lightGray])
     textField.attributedPlaceholder = str
    

    In Objective C

    NSString *placeHolder = [textField.placeholder length]>0 ? textField.placeholder: @"";
    NSAttributedString *str = [[NSAttributedString alloc] initWithString:placeHolder attributes:@{ NSForegroundColorAttributeName : [UIColor lightGrayColor] }];
    textField.attributedPlaceholder = str;
    
    0 讨论(0)
  • 2020-11-29 16:13

    For Swift

    func setPlaceholderColor(textField: UITextField, placeholderText: String) {
        textField.attributedPlaceholder = NSAttributedString(string: placeholderText, attributes: [NSForegroundColorAttributeName: UIColor.pelorBlack])
    }
    

    You can use this;

    self.setPlaceholderColor(textField: self.emailTextField, placeholderText: "E-Mail/Username")
    
    0 讨论(0)
  • 2020-11-29 16:14

    In Swift 3.0, Use

    let color = UIColor.lightText
    textField.attributedPlaceholder = NSAttributedString(string: textField.placeholder, attributes: [NSForegroundColorAttributeName : color])
    

    In Siwft 5.0 + Use

    let color = UIColor.lightText
    let placeholder = textField.placeholder ?? "" //There should be a placeholder set in storyboard or elsewhere string or pass empty
    textField.attributedPlaceholder = NSAttributedString(string: placeholder, attributes: [NSAttributedString.Key.foregroundColor : color])
    
    0 讨论(0)
  • 2020-11-29 16:14

    for iOS13

    +(void)ChangeplaceholderColor :(UITextField *)TxtFld andColor:(UIColor*)color { 
        NSMutableAttributedString *placeholderAttributedString = [[NSMutableAttributedString alloc] initWithAttributedString:TxtFld.attributedPlaceholder];
           [placeholderAttributedString addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, [placeholderAttributedString length])];
           TxtFld.attributedPlaceholder = placeholderAttributedString;
    
    }
    
    0 讨论(0)
提交回复
热议问题