Consider the _highlightResult’s value: "The Shawshank <em>Red</em>emption".
The part of the string to highlight is marked with <em> tags or your custom tags.HighlightedString is constructed with a raw tagged string, detects the tags and creates a TaggedString. This tagged string provides following properties:
input. The input string.
output. The input string without its tags.
taggedRanges. A list of ranges defining highlighted ranges in output.
You can build a highlighted string in iOS is with an NSAttributedString.
InstantSearch provides HighlightedString and NSAttributedString extensions to simplify this.
Swift
Report incorrect code
Copy
let rawHighlightedString = "The Shawshank <em>Red</em>emption"let highlightedString = HighlightedString(string: rawHighlightedString)// Attributes to apply for a highlighted part of the stringlet highLightingAttributes: [NSAttributedString.Key: Any] = [ .foregroundColor: UIColor.red]// Create attributed string highlighted part of which is redlet attributedString = NSAttributedString(highlightedString: highlightedString, attributes: attributes)
The produced NSAttributedString can be assigned to a UIKit component that supports it.
The Hit wrapper structure uses the HighlightedString structure.
You can extract a highlighted string for an attribute using its hightlightedString(forKey key: String) function.Example: