我的要求是,当文本字段的占位符可见时,它的颜色应该是灰色,字体大小为 10,但是当用户开始输入文本字段时,它的颜色应该是黑色,字体大小为 14。我试过这个:

textField.attributedPlaceholder = NSAttributedString(string: placeholderText,
                                                    attributes: [NSAttributedString.Key.foregroundColor: Color.iPhoneGrayColor, NSAttributedString.Key.font: UIFont(name: "SourceSansPro-Regular", size: 10)!])
    textField.textColor = UIColor.black
    textField.font = UIFont(name: "SourceSansPro-Regular", size: 14)!

但是,我的占位符字体大小被 textfield.font 覆盖,所以我无法获得大小为 10 的占位符。我哪里出错了?现在尝试了几个小时。任何帮助将不胜感激。

最佳答案

只需在 设置字体后设置占位符 ,因为设置字体也适用于占位符(请参阅 https://developer.apple.com/documentation/uikit/uitextfield/1619604-font ):

textField.font = ...
textField.textColor = ...

textField.attributedPlaceholder = ...

关于ios - UITextField 占位符和文本大小应该分开 - Swift,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54418571/

10-12 14:36
查看更多