问题
I'm having trouble bolding any characters between an indicated pair of "**" characters. 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:@"\\*([^**]+)\\*" options:NSRegularExpressionSearch|NSCaseInsensitiveSearch];
if (range.location != NSNotFound) {
[mutableAttributedString setAttributes:@{NSFontAttributeName:[UIFont fontWithName:AGHeavyFontName size:size]} range:range];
}
[[mutableAttributedString mutableString] replaceOccurrencesOfString:@"*" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, queryString.length)];
return mutableAttributedString;
I'm having issues- this code will still catch characters with one pair of "*"'s, so in this case,
"The fox has ran *around the corner*
will still read as "The fox has ran around the corner", when it shouldnt.
Any ideas?
回答1:
Perhaps this may help you:
http://regex101.com/r/eF6pJ8
\\*{2}([^*]*)\\*{2}
来源:https://stackoverflow.com/questions/21103445/how-to-catch-special-indicated-characters-in-an-nsstring-and-bold-whats-in