更改NSAttributedString中子string的属性

这个问题可能是这个问题的重复。 但是答案不适合我,我想要更具体。

我有一个NSString ,但我需要一个NS(Mutable)AttributedString和这个string中的一些单词应给予不同的颜色。 我试过这个:

 NSString *text = @"This is the text and i want to replace something"; NSDictionary *attributes = @ {NSForegroundColorAttributeName : [UIColor redColor]}; NSMutableAttributedString *subString = [[NSMutableAttributedString alloc] initWithString:@"AND" attributes:attributes]; NSMutableAttributedString *newText = [[NSMutableAttributedString alloc] initWithString:text]; newText = [[newText mutableString] stringByReplacingOccurrencesOfString:@"and" withString:[subString mutableString]]; 

“和”应该是大写的红色。

该文件说,mutableString保持属性映射。 但是用我的替代品,我在赋值的右侧(在我的代码片段的最后一行)没有更多的attributesString。

我怎样才能得到我想要的? ;)

@ Hyperlord的答案是有效的,但是只有在inputstring中出现一个“and”字样的情况下。 无论如何,我会做的是使用NSString的stringByReplacingOccurrencesOfString:最初将每个“和”更改为“与”,然后使用一些正则expression式检测属性string匹配,并在该范围应用NSForegroundColorAttributeName 。 这是一个例子:

 NSString *initial = @"This is the text and i want to replace something and stuff and stuff"; NSString *text = [initial stringByReplacingOccurrencesOfString:@"and" withString:@"AND"]; NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithString:text]; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(AND)" options:kNilOptions error:nil]; NSRange range = NSMakeRange(0,text.length); [regex enumerateMatchesInString:text options:kNilOptions range:range usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) { NSRange subStringRange = [result rangeAtIndex:1]; [mutableAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:subStringRange]; }]; 

最后,只需将属性string应用到您的标签。

 [myLabel setAttributedText:mutableAttributedString]; 

我想你应该使用现有的NSString创build一个NSMutableAttributedString ,然后添加适当的NSRange的样式属性,以着色你想要强调的部分,例如:

 NSString *text = @"This is the text and i want to replace something"; NSMutableAttributedString *mutable = [[NSMutableAttributedString alloc] initWithString:text]; [mutable addAttribute: NSForegroundColorAttributeName value:[UIColor redColor] range:[text rangeOfString:@"and"]]; 

请注意:这只是从我的头上,没有经过testing;-)

这里有另外一个实现(在Swift中),如果你使用你的属性string进行一些更复杂的操作(如添加/删除字符)会有帮助:

 let text = "This is the text and i want to replace something" let mutAttrStr = NSMutableAttributedString(string: text) let pattern = "\\band\\b" let regex = NSRegularExpression(pattern: pattern, options: .allZeros, error: nil) while let result = regex!.firstMatchInString(mutAttrStr.string, options: .allZeros, range:NSMakeRange(0, count(mutAttrStr.string)) { let substring = NSMutableAttributedString(attributedString: mutAttrStr.attributedSubstringFromRange(result.range)) // manipulate substring attributes here substring.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range NSMakeRange(0, count(substring.string)) mutAttrStr.replaceCharactersInRange(result.range, withAttributedString: substring) } 

您的最终归因string应该是:

 let finalAttrStr = mutAttrStr.copy() as! NSAttributedString 

请在Swift 2中试试这个代码

 var someStr = "This is the text and i want to replace something" someStr.replaceRange(someStr.rangeOfString("and")!, with: "AND") let attributeStr = NSMutableAttributedString(string: someStr) attributeStr.setAttributes([NSForegroundColorAttributeName: UIColor.yellowColor()], range: NSMakeRange(17, 3) ) testLbl.attributedText = attributeStr 
Interesting Posts