问题
I'm having an issue finding multiple groups of substrings indicated by a pair of ** characters, and bolding them. for example in this NSString:
The Fox has **ran** around the **corner**
should read: The fox has ran around the corner
here is my code:
NSString *questionString = queryString;
NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithString:questionString];
NSRange range = [questionString rangeOfString:@"\\*{2}([^*]+)\\*{2}" options:NSRegularExpressionSearch];
if (range.location != NSNotFound) {
[mutableAttributedString setAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:size]} range:range];
}
[[mutableAttributedString mutableString] replaceOccurrencesOfString:@"**" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, queryString.length)];
return mutableAttributedString;
this code only catches one pair of indicated characters, so all i get back is :The fox has ran around the corner
any ideas?
回答1:
You have to enumerate all matches of the regular expression. It is a bit tricky because all ranges shift when you remove the limiting "**" pairs.
This seems to do the job:
NSString *questionString = @"The Fox has **ran** around the **corner**";
NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithString:questionString];
NSError *error = nil;
NSString *pattern = @"(\\*{2})([^*]+)(\\*{2})";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];
__block NSUInteger shift = 0; // number of characters removed so far
[regex enumerateMatchesInString:questionString options:0 range:NSMakeRange(0, [questionString length])
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
NSRange r1 = [result rangeAtIndex:1]; // location of first **
NSRange r2 = [result rangeAtIndex:2]; // location of word in between
NSRange r3 = [result rangeAtIndex:3]; // location of second **
// Adjust locations according to the string modifications:
r1.location -= shift;
r2.location -= shift;
r3.location -= shift;
// Set attribute for the word:
[mutableAttributedString setAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:12.0]} range:r2];
// Remove the **'s:
[[mutableAttributedString mutableString] deleteCharactersInRange:r3];
[[mutableAttributedString mutableString] deleteCharactersInRange:r1];
// Update offset:
shift += r1.length + r3.length;
}];
Result (in debugger console):
The Fox has {
}ran{
NSFont = "<UICTFont: 0xc03efb0> font-family: \".HelveticaNeueInterface-MediumP4\"; font-weight: bold; font-style: normal; font-size: 12.00pt";
} around the {
}corner{
NSFont = "<UICTFont: 0xc03efb0> font-family: \".HelveticaNeueInterface-MediumP4\"; font-weight: bold; font-style: normal; font-size: 12.00pt";
}
来源:https://stackoverflow.com/questions/21124035/how-to-catch-multiple-instances-special-indicated-characters-in-an-nsstring