I have a tableview. The purpose of the tableview is to show search results originating from UISearchBar user input. The tableview has a custom tableview cell in which there is a Label and UITextView. A function passes in the user search text, matches said search text against some base text and attributes only the matched text using a specific font, font color and size. It serves the purpose of highlighting the matched characters so that the app user can see the search text in the Label and UITextView of the tableview cells. Of note, the base font of the Label and UITextView are set in IB. The matched search text font, color and size are set through the function referenced above.
On execution, everything works perfectly with respect to the text appearing in the Label. The matched search text is changed to a color, text and size as intended and the unmatched text stays unchanged.
The latter is not the case with the text displayed in the UITextView. The color, size and font of the matched search text changes as intended. For example, if the app user enters "the" into the UISearchBar, any instance of "the" shown in the UITextView has the intended text color, font type and size change. However, the font of the text in the UITextView (unmatched text) is changed to something completely different. (EDIT - my original post stated that matched and unmatched did not change). As to the unmatched text, the text is changed to a font type that is completely different than anything set in IB or set via the function and the font size is small. The font type and size seem immutable despite my tinkering.
Here is the code applied to the UITextView object:
// class function used to match search text and set attribute
class func attributeSearchedText (searchString: String, baseString: String) -> NSMutableAttributedString {
let attributedString:NSMutableAttributedString = NSMutableAttributedString(string: baseString)
let regex = try! NSRegularExpression(pattern: searchString, options: .caseInsensitive)
for match in regex.matches(in: baseString, options: NSRegularExpression.MatchingOptions(), range: NSRange(location: 0, length: (baseString.characters.count))) as [NSTextCheckingResult]
{
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.orange, range: match.range)
attributedString.addAttribute(NSFontAttributeName, value: UIFont(name:"HelveticaNeue-Bold", size: 14.0)!, range: match.range)
}
return attributedString
}
// function applied to the tableview cell
cell.ruleTextBody.attributedText = SearchObject.attributeSearchedText2(searchString: searchString, baseString: filteredData[indexPath.row].ruleTxtBody)
If I remove the attributedText function then the font originally set in IB is shown as intended:
cell.ruleTextBody.text = filteredData[indexPath.row].ruleTxtBody
In view of the foregoing, it is clear that the issue has something to do with attributedText and UITextView. I need the UITextView as I want to keep the scroll feature.
Any thoughts on how to fix this issue?