MDCTextField中的属性文本不起作用

时间:2019-03-28 12:40:45

标签: ios swift

我的日期格式例如“今天28. 3. 2019”,我试图将第一个单词(“今天”)的颜色设置为灰色,但是代码不起作用,颜色也不会更改。更改非属性文字的颜色效果很好。您有什么想法可能会出问题吗?

谢谢

编辑:我们将MaterialComponents窗格用于TextFields

func setStartDate(_ date: String?) {
        if let date = date {
            let firstWord = date.components(separatedBy: " ").first
            let firstWordRange = (date as NSString).range(of: firstWord!)
            let attributedDate = NSMutableAttributedString(string: date)

            attributedDate.addAttribute(NSAttributedString.Key.foregroundColor,
                                        value: UIColor.gray,
                                        range: firstWordRange)

            startDateBox.input.attributedText = attributedDate
        }
    startDateBox.input.text = date
    }

1 个答案:

答案 0 :(得分:0)

删除简单的文本分配startDateBox.input.text = date

休息很好

 func setStartDate(_ date: String?) {
            if let date = date {
                let firstWord = date.components(separatedBy: " ").first
                let firstWordRange = (date as NSString).range(of: firstWord!)
                let attributedDate = NSMutableAttributedString(string: date)

                attributedDate.addAttribute(NSAttributedString.Key.foregroundColor,
                                            value: UIColor.gray,
                                            range: firstWordRange)

                startDateBox.input.attributedText = attributedDate
            }
        }