Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Highlighter/Highlightable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public protocol Highlightable: class {
var attributedTextValue: NSAttributedString? { get set }

func highlight(text: String, normal normalAttributes: [NSAttributedString.Key : Any]?, highlight highlightAttributes: [NSAttributedString.Key : Any]?)
func unHighlight()
}

extension Highlightable {
Expand All @@ -31,4 +32,10 @@ extension Highlightable {
highlight: highlightAttributes
)
}

public func unHighlight() {
if let attText = self.attributedTextValue {
attributedTextValue = NSAttributedString(string: attText.string)
}
}
}
9 changes: 9 additions & 0 deletions Highlighter/HighlightableContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public protocol HighlightableContainer: class {
/// - highlightAttributes: Attributes to apply (highlight) to matching text
/// - type: (optional) Only search `Highlightable`s of this type
func highlight(text: String, normal normalAttributes: [NSAttributedString.Key : Any]?, highlight highlightAttributes: [NSAttributedString.Key : Any]?, type: Highlightable.Type?)
func unHighlight(type: Highlightable.Type?)
}

extension HighlightableContainer {
Expand All @@ -27,6 +28,14 @@ extension HighlightableContainer {
.filter { return type == nil || Swift.type(of: $0) == type }
.forEach { $0.highlight(text: text, normal: normalAttributes, highlight: highlightAttributes) }
}

public func unHighlight(type: Highlightable.Type? = nil) {
let mirror = Mirror(reflecting: self)
mirror.children
.compactMap { $0.value as? Highlightable }
.filter { return type == nil || Swift.type(of: $0) == type }
.forEach { $0.unHighlight() }
}
}

extension UIView: HighlightableContainer { }