Changing Placeholder Text Color with Swift

前端 未结 30 2535
独厮守ぢ
独厮守ぢ 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: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;
    

提交回复
热议问题