我正在嘗試為 AttributedString 中的下劃線文本賦予不同的顏色。按照這個例子。

但是這段代碼的結果是:
var attributedString = try AttributedString(markdown: self)
if let rangeWord = self.slice(from: "[", to: "]") {
let range = attributedString.range(of: rangeWord)!
attributedString[range].underlineStyle = .single
attributedString[range].foregroundColor = .white
attributedString[range].underlineColor = .green
}
不是預期的

有解決方法嗎?
uj5u.com熱心網友回復:
嘗試NSAttributedString同樣使用:
let labelString = "your label"
let textColor: UIColor = .black
let underLineColor: UIColor = .green
let underLineStyle = NSUnderlineStyle.single.rawValue
let labelAtributes:[NSAttributedString.Key : Any] = [
NSAttributedString.Key.foregroundColor: textColor,
NSAttributedString.Key.underlineStyle: underLineStyle,
NSAttributedString.Key.underlineColor: underLineColor
]
let underlineAttributedString = NSAttributedString(string: labelString,
attributes: labelAtributes)
label.attributedText = underlineAttributedString
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/480227.html
標籤:IOS 迅速 nsattributedstring 属性字符串
上一篇:當月份選擇器值更改時,我的日期選擇器值將使用xamarinC#在數字選擇器中更改
下一篇:在R中創建具有排他條件的變數
