例如我必須把一個字符串「你好,世界!」在UIButton
。屬性文本的顏色如何繼承父級顏色?
而且「你好,」和「世界!」應該是不同的顏色。
「你好,」默認顏色爲UIButton
,「世界!」有由NSMutableAttributedString
定製的顏色。
我再次解釋代碼和結果。
// Using a library `SwiftyAttributes`(https://github.com/eddiekaiger/SwiftyAttributes)
button.attributedText = "Hello, ".withJust() + "world!".withTextColor(.red)
button.setTitleColor(.black, for: .normal)
我想:你好,(黑色)世界(紅色)
button.setTitleColor(.red, for: .normal)
我想:!你好,(紅)世界(紅色)
button.setTitleColor(.blue, for: .normal)
我想要:你好,(藍色)世界!
但始終結果是:你好,(黑色)世界(紅色)
...
也許我想的NSMutableAttributedString
默認顏色的優先級高於UIButton
默認顏色高。
但是,我可以看到我想要的結果嗎?