Highlight words in between response dynamic data names
"Work for Joy Bag is Started"
"Work for Jack & Jill is Ended"
"Work for Uncle Sam is Started"
Need to highlight "Joy Bag", "Jack & Sam" and "Uncle Same" with bold and rest in regular font.
class StudentCell: UITableViewCell
var welcomeData: StudentListModel.Data?{
didSet{
setCellData()
}
}
func setCellData() {
if let titleValue = welcomeData?.title{
let attributes = [[NSAttributedString.Key.foregroundColor:UIColor.red], [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 17)]]
nameValue.attributedText = titleValue.highlightWordsIn(highlightedWords: "Work for is Started", attributes: attributes)
}
}
extension String {
func highlightWordsIn(highlightedWords: String, attributes: [[NSAttributedString.Key: Any]]) -> NSMutableAttributedString {
let range = (self as NSString).range(of: highlightedWords)
let result = NSMutableAttributedString(string: self)
for attribute in attributes {
result.addAttributes(attribute, range: range)
}
return result
}
}
From Given String "Work for Joy Bag is Started" In Between Work for - is Started or is Ended need to be Highlighted?