UITextField上的奇怪的快速行为

时间:2015-05-08 09:14:43

标签: ios iphone swift

方案: 我为 UITextField 设置了文本字体和大小。 他们也有占位符。 下面的代码在我的UIViewController中:

        let font = UIFont(name: "FlamaSemicondensed-Book", size: 18)

        let attributesDictionary = [NSForegroundColorAttributeName: DefaultSystemColor.DarkRed]

        // Textfileds without borders (and placeholders)
        UsernameTextField.borderStyle = UITextBorderStyle.None
        UsernameTextField.font = font
        UsernameTextField.textColor = DefaultSystemColor.DarkRed
        UsernameTextField.attributedPlaceholder = NSAttributedString(string: "Email", attributes: attributesDictionary)

我配置(在AppDelegate中)一个全局UI设置,它将我的所有 UILabels 格式化为某个字体大小。 下面的代码在我的GlobalUISettings类中:

    let font = UIFont(name: "FlamaSemicondensed-Book", size: 13)!
    var labelAppearace = UILabel.appearance()
    labelAppearace.font = font

这里有什么奇怪之处:<​​/ p>

当选择了这个UITextField时,我输入的格式是我为文本字段设置的格式(占位符也可以)。

但是当我离开战场时,它会假定UILabel的行为。

我知道,因为如果我评论UILabel格式,textField就能正常工作。

有人知道为什么会这样吗?

2 个答案:

答案 0 :(得分:0)

尝试将let font更改为let labelFont,因为'font'是全局的。它可能正在影响。

答案 1 :(得分:0)

这是正确的行为。

要更改占位符的字体,您必须将NSFontAttributeName属性与所需字体作为值添加到NSAttributedString分配给attributedPlaceholder

示例:

let attributesDictionary = [
    NSFontAttributeName: font,
    NSForegroundColorAttributeName: DefaultSystemColor.DarkRed
]
相关问题