Setup NSTextAttachmentViewProvider

This commit is contained in:
aringenbach
2022-04-27 11:55:23 +02:00
parent 5c0ed53401
commit 3d4657fc3b
12 changed files with 235 additions and 98 deletions
+20
View File
@@ -24,4 +24,24 @@ public extension NSAttributedString {
result.removeAttribute(.link, range: NSRange(location: 0, length: length))
return result
}
/// Enumerate attribute for given key and conveniently ignore any attribute that doesn't match given generic type.
///
/// - Parameters:
/// - attrName: The name of the attribute to enumerate.
/// - enumerationRange: The range over which the attribute values are enumerated.
/// - opts: The options used by the enumeration. For possible values, see NSAttributedStringEnumerationOptions.
/// - block: The block to apply to ranges of the specified attribute in the attributed string.
func vc_enumerateAttribute<T>(_ attrName: NSAttributedString.Key,
in enumerationRange: NSRange,
options opts: NSAttributedString.EnumerationOptions = [],
using block: (T, NSRange, UnsafeMutablePointer<ObjCBool>) -> Void) {
self.enumerateAttribute(attrName,
in: enumerationRange,
options: opts) { (attr: Any?, range: NSRange, stop: UnsafeMutablePointer<ObjCBool>) in
guard let typedAttr = attr as? T else { return }
block(typedAttr, range, stop)
}
}
}