18

这个问题可能是这个问题的重复。但答案对我不起作用,我想更具体一些。

我有 a NSString,但我需要 aNS(Mutable)AttributedString并且这个字符串中的一些单词应该被赋予不同的颜色。我试过这个:

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]];

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

文档说 mutableString 保留属性映射。但是用我的替换物,我在分配的右侧没有更多的属性字符串(在我的代码片段的最后一行)。

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

4

4 回答 4

36

@Hyperlord 的答案将起作用,但前提是输入字符串中出现“and”一词。无论如何,我要做的是stringByReplacingOccurrencesOfString:最初使用 NSString 将每个“and”更改为“AND”,然后使用一点正则表达式来检测属性字符串中的匹配,并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];
}];

最后,只需将属性字符串应用于您的标签。

[myLabel setAttributedText:mutableAttributedString];
于 2013-07-05T11:34:07.403 回答
19

NSMutableAttributedString我认为您应该使用现有的创建一个NSString,然后添加适当的样式属性,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"]];

请注意:这只是我的想法,根本没有经过测试;-)

于 2013-07-05T11:07:29.163 回答
1

请在 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
于 2016-01-25T13:55:25.860 回答
0

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

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)
}

您的最终属性字符串应为:

let finalAttrStr = mutAttrStr.copy() as! NSAttributedString
于 2015-08-24T20:12:00.500 回答