此问题可能与this one重复。但答案对我不起作用,我希望更具体。
我有一个NSString
,但我需要NS(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]];
“和”应该是大写的红色。
文档说mutableString保留属性映射。但是对于我的替换事物,我在赋值的右侧(在我的代码片段的最后一行)中没有更多的claimString。
我怎样才能得到我想要的东西? ;)
答案 0 :(得分:35)
@Hyperlord的答案将起作用,但前提是输入字符串中出现单词“and”。无论如何,我要做的是最初使用NSString的stringByReplacingOccurrencesOfString:
将每个“和”更改为“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];
答案 1 :(得分:18)
我认为您应该使用现有的NSMutableAttributedString
创建NSString
,然后使用相应的NSRange
添加样式属性,以便为要强调的部分着色,例如:< / p>
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"]];
请注意:这只是我的想法,根本没有经过测试; - )
答案 2 :(得分: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
答案 3 :(得分:0)
请在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