Swift: AttributedString 不同颜色的下划线
Swift: AttributedString underline with different color
我正在尝试为 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
}
不是预期的
有解决办法吗?
同样尝试使用 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
我正在尝试为 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
}
不是预期的
有解决办法吗?
同样尝试使用 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