我需要用plist中的字符串替换DFT这样的单词和注册商标符号。
我的代码有效,但总是在同一个字符串中弄乱第二个:
- (NSAttributedString *)addRegisteredTrademarkTo:(NSString*)text
{
NSMutableAttributedString *rawAttString=[[NSMutableAttributedString alloc] initWithString:text];
UIFont *boldFont = [UIFont boldSystemFontOfSize:8];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"®"
attributes:@{NSFontAttributeName:boldFont,
NSForegroundColorAttributeName:[UIColor blackColor],
NSBaselineOffsetAttributeName : @4,
NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:5]
}];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"DRT" options:kNilOptions error:nil];
NSRange range = NSMakeRange(0,rawAttString.length);
[regex enumerateMatchesInString:text options:kNilOptions range:range usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
NSRange subStringRange = [result rangeAtIndex:0];
[rawAttString replaceCharactersInRange:subStringRange withAttributedString:attributedString];
}];
NSAttributedString *t = [[NSAttributedString alloc] initWithAttributedString:rawAttString];
return t;
}
字符串是:Fake name™,具有另一个FakeDRT构建访问权限。 SionDRT电梯尺寸 输出为:
第二个符号总是放错地方 - 我无法弄清楚原因。有什么指针吗?