编辑后UITextView文本颜色更改

时间:2016-01-21 16:58:06

标签: swift uitextfield tvos uitextfielddelegate

所以我有一个问题,我已经制作了UITextView的子类来改变聚焦前后的各种颜色。我发现在实际输入文本后它几乎忽略了我指定的文本颜色。

我的代码如下所示。请注意,我有一个名为colors的字典,它将状态(“聚焦”或“未聚焦”)映射到另一个包含字段背景和文本颜色的字典

func setColors(state:String) {
        self.attributedPlaceholder = NSAttributedString(string: (self.attributedPlaceholder?.string)!, attributes:  [NSForegroundColorAttributeName: self.colors[state]!["text"]!])
        self.backgroundColor = self.colors[state]!["background"]
        self.layer.borderColor = self.colors[state]!["background"]!.CGColor
        self.textColor = self.colors[state]!["text"]
        self.tintColor = self.colors[state]!["text"]
    }

override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
        super.didUpdateFocusInContext(context, withAnimationCoordinator: coordinator)

        if context.nextFocusedView == self {
            coordinator.addCoordinatedAnimations({ () -> Void in
                self.setColors("focused")
            }, completion: nil)
        }
        else {
            coordinator.addCoordinatedAnimations({ () -> Void in
                self.setColors("unfocused")
            }, completion: nil)
        }
    }

所以在我输入任何内容之前它看起来很好。在视图上的字段之间切换颜色都看起来正确。然而,当我选择一个字段并在之后输入文字时,唯一看起来正确的是背景颜色,当聚焦/未聚焦时,它会适当地改变。是否在输入文本后设置了另一个文本颜色属性?

0 个答案:

没有答案