我有 UITextView text_recepty,我在其中用不同的顏色撰寫文本。在我的解決方案中,我需要添加彩色文本,但原始文本和顏色必須保持不變。有沒有辦法正確獲取帶有顏色的原始文本?
var toastcolor: UIColor = UIColor.black
var toast: String = "TEST..."
if ChodButtonisOn == true {
toastcolor = UIColor(red: 118/255, green: 110/255, blue: 38/255, alpha: 1.0)
} else {
if HalfButtonisOn == true {
toastcolor = UIColor(red: 240/255, green: 127/255, blue: 18/255, alpha: 1.0)
}
}
let text1 = NSMutableAttributedString(string: text_recepty.text!)
let attr2 = [NSAttributedString.Key.foregroundColor: toastcolor]
let text2 = NSAttributedString(string: toast "\n", attributes: attr2)
text1.append(text2)
text_recepty.attributedText = text1
```
uj5u.com熱心網友回復:
您需要attributedText從 UITextView獲取,然后向其附加新文本,例如:
let existingText = myTextView.attributedText
let mutableText = NSAttributedString(attributedString: existingText)
// Then add your text
檢查NSMutableAttributedString檔案以了解如何執行最后一部分。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/337916.html
