The text of the label in ios is displayed in multiple colors.
1. In the initialization method, add all labels with the keywords to be highlighted to the labels array and add the colors of these labels in the original font to labelTextColors. The Code is as follows:
Self. labels = [NSArray arrayWithObjects: self. textLabel, self. detailTextLabel, nil];
Self. labelTextColors = [NSArray arrayWithObjects: kColor_Black, kColor_LightGray, nil];
Ii. Add this code in layoutSubviews
For (int I = 0; I <self. labels. count; I ++ ){
UILabel * label = self. labels [I];
// 1. Retrieve the color of the original label font.
UIColor * originalColor = self. labelTextColors [I];
// 2. restore the original color
NSMutableAttributedString * originalMat = label. attributedText. mutableCopy;
[OriginalMat setAttributes: @ {NSForegroundColorAttributeName: originalColor}
Range: [label. text rangeOfString: label. text];
Label. attributedText = originalMat;
// 3. Highlight keywords
NSMutableAttributedString * mat = label. attributedText. mutableCopy;
[Mat setAttributes: @ {NSForegroundColorAttributeName: [UIColor blueColor]}
Range: [label. text rangeOfString: self. keyWords]; // keyWords are the keyWords to be highlighted.
Label. attributedText = mat;
}