斯威夫特 5 – 4
let attributedText = NSAttributedString(string: "Hello, playground", attributes: [
.foregroundColor: UIColor.red,
.backgroundColor: UIColor.green,
.ligature: 1,
.strikethroughStyle: 1
])
// retrieve attributes
let attributes = attributedText.attributes(at: 0, effectiveRange: nil)
// iterate each attribute
for attr in attributes {
print(attr.key, attr.value)
}
如果您已经定义了attributedText
的标签。
Swift 3
var attributes = attributedText.attributes(
at: 0,
longestEffectiveRange: nil,
in: NSRange(location: 0, length: attributedText.length)
)
斯威夫特2.2
var attributes = attributedText.attributesAtIndex(0,
longestEffectiveRange: nil,
inRange: NSMakeRange(0, attributedText.length)
)