This refactoring will be easier after #109 is done.
I need to move this out of SearchView.swift, perhaps by turning it into a function:
private var attributedString: AttributedString {
var attributedString = AttributedString(quote.text)
let searchTextLowercased = (searchText ?? "").lowercased()
let textLowercased = quote.text.lowercased()
var searchStartIndex = textLowercased.startIndex
// Loop to find and highlight all occurrences
while let range = textLowercased.range(of: searchTextLowercased, range: searchStartIndex..<textLowercased.endIndex) {
// Convert String.Index to AttributedString.Index
if let attributedRange = Range(NSRange(range, in: textLowercased), in: attributedString) {
attributedString[attributedRange].backgroundColor = (colorPalettes[safe: sharedVars.colorPaletteIndex]?[2] ?? .yellow).opacity(0.3)
}
// Move searchStartIndex to the end of the found range to continue searching
searchStartIndex = range.upperBound
}
return attributedString
}
This refactoring will be easier after #109 is done.
I need to move this out of
SearchView.swift
, perhaps by turning it into a function: