1 运用 func attribute(_ attrName: NSAttributedStringKey, at location: Int, effectiveRange range: NSRangePointer?) -> Any?
如查找文字的颜色
let color = attributedText.attribute(NSForegroundColorAttributeName, at: 0, effectiveRange: nil) as! UIColor
2 运用func enumerateAttributes(in enumerationRange: NSRange, options opts: NSAttributedString.EnumerationOptions = [], using block: ([NSAttributedStringKey : Any], NSRange, UnsafeMutablePointer<ObjCBool>) -> Void) 查找文字是否加粗 斜体
self.attributedText.enumerateAttributes(in: rangeToCheck, options: NSAttributedString.EnumerationOptions(rawValue: 0)) { (object, range, stop) in
if object.keys.contains(NSFontAttributeName) {
//检查包含的元素
if let font = object[NSFontAttributeName] as? UIFont {
if font.isBold {
isBold = true
print("Bold attribute")
}
if font.isItalic {
isItalic = true
print("Italic attribute")
}
if object.keys.contains(NSUnderlineStyleAttributeName) {
isUnderlined = true
print("Underline attribute")
}
}
}