I have done this by creating a category for NSMutableAttributedString
-(void)setColorForText:(NSString*) textToFind withColor:(UIColor*) color
{
NSRange range = [self.mutableString rangeOfString:textToFind options:NSCaseInsensitiveSearch];
if (range.location != NSNotFound) {
[self addAttribute:NSForegroundColorAttributeName value:color range:range];
}
}
像这样使用它
- (void) setColoredLabel
{
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"Here is a red blue and green text"];
[string setColorForText:@"red" withColor:[UIColor redColor]];
[string setColorForText:@"blue" withColor:[UIColor blueColor]];
[string setColorForText:@"green" withColor:[UIColor greenColor]];
mylabel.attributedText = string;
}
SWIFT 3
extension NSMutableAttributedString{
func setColorForText(_ textToFind: String, with color: UIColor) {
let range = self.mutableString.range(of: textToFind, options: .caseInsensitive)
if range.location != NSNotFound {
addAttribute(NSForegroundColorAttributeName, value: color, range: range)
}
}
}
USAGE
func setColoredLabel() {
let string = NSMutableAttributedString(string: "Here is a red blue and green text")
string.setColorForText("red", with: #colorLiteral(red: 0.9254902005, green: 0.2352941185, blue: 0.1019607857, alpha: 1))
string.setColorForText("blue", with: #colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1))
string.setColorForText("green", with: #colorLiteral(red: 0.3411764801, green: 0.6235294342, blue: 0.1686274558, alpha: 1))
mylabel.attributedText = string
}
SWIFT 4 @kj13 Thanks for notifying
// If no text is send, then the style will be applied to full text
func setColorForText(_ textToFind: String?, with color: UIColor) {
let range:NSRange?
if let text = textToFind{
range = self.mutableString.range(of: text, options: .caseInsensitive)
}else{
range = NSMakeRange(0, self.length)
}
if range!.location != NSNotFound {
addAttribute(NSAttributedStringKey.foregroundColor, value: color, range: range!)
}
}
I have did more experiments with attributes and below are the results, here is theSOURCECODE
//NSString *myString = @"I have to replace text 'Dr Andrew Murphy, John Smith' ";
NSString *myString = @"Not a member?signin";
//Create mutable string from original one
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:myString];
//Fing range of the string you want to change colour
//If you need to change colour in more that one place just repeat it
NSRange range = [myString rangeOfString:@"signin"];
[attString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:(63/255.0) green:(163/255.0) blue:(158/255.0) alpha:1.0] range:range];
//Add it to the label - notice its not text property but it's attributeText
_label.attributedText = attString;
对于 Xamarin用户,我有一个静态 C # 方法,其中我传入一个字符串数组、一个 UIColours 数组和 UIFont 数组(它们需要匹配长度)。然后将属性化字符串传递回来。
see:
public static NSMutableAttributedString GetFormattedText(string[] texts, UIColor[] colors, UIFont[] fonts) {
NSMutableAttributedString attrString = new NSMutableAttributedString(string.Join("", texts));
int position = 0;
for (int i = 0; i < texts.Length; i++) {
attrString.AddAttribute(new NSString("NSForegroundColorAttributeName"), colors[i], new NSRange(position, texts[i].Length));
var fontAttribute = new UIStringAttributes {
Font = fonts[I]
};
attrString.AddAttributes(fontAttribute, new NSRange(position, texts[i].Length));
position += texts[i].Length;
}
return attrString;
}
我的答案也有颜色选项,可以选择所有出现的文字不只一个出现它: “ wa ba wa ba dubdub”,你可以颜色所有出现的 wa 不只是第一个出现像接受的答案。
extension NSMutableAttributedString{
func setColorForText(_ textToFind: String, with color: UIColor) {
let range = self.mutableString.range(of: textToFind, options: .caseInsensitive)
if range.location != NSNotFound {
addAttribute(NSForegroundColorAttributeName, value: color, range: range)
}
}
func setColorForAllOccuranceOfText(_ textToFind: String, with color: UIColor) {
let inputLength = self.string.count
let searchLength = textToFind.count
var range = NSRange(location: 0, length: self.length)
while (range.location != NSNotFound) {
range = (self.string as NSString).range(of: textToFind, options: [], range: range)
if (range.location != NSNotFound) {
self.addAttribute(NSForegroundColorAttributeName, value: color, range: NSRange(location: range.location, length: searchLength))
range = NSRange(location: range.location + range.length, length: inputLength - (range.location + range.length))
}
}
}
}
现在你可以这样做:
let message = NSMutableAttributedString(string: "wa ba wa ba dubdub")
message.setColorForText(subtitle, with: UIColor.red)
// or the below one if you want all the occurrence to be colored
message.setColorForAllOccuranceOfText("wa", with: UIColor.red)
// then you set this attributed string to your label :
lblMessage.attributedText = message