如何在NSString中捕获特殊指示的**字符**并加粗它们之间的内容? [英] How to catch special indicated **characters** in an NSString and bold what's in between?
问题描述
我无法在指定的一对**字符之间加粗任何字符。例如,在这个NSString中:
狐狸在拐角处跑了**。
应为:狐狸跑到左右角落> p> 这是我的代码: 我遇到问题 - 这段代码仍会捕获一对*的字符,所以在这种情况下, 仍然会显示为狐狸已经跑到左右角落,当它不应该时。 任何想法? 也许这可以帮到你: I'm having trouble bolding any characters between an indicated pair of "**" characters. For example, in this NSString: should read: "The fox has ran around the corner" here is my code : I'm having issues- this code will still catch characters with one pair of "*"'s, so in this case, will still read as "The fox has ran around the corner", when it shouldnt. Any ideas? Perhaps this may help you: 这篇关于如何在NSString中捕获特殊指示的**字符**并加粗它们之间的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
NSString * questionString = queryString;
NSMutableAttributedString * mutableAttributedString = [[NSMutableAttributedString alloc] initWithString:questionString];
NSRange range = [questionString rangeOfString:@\\ *([^ **] +)\\ *选项: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;
狐狸已经跑到了拐角处*
\\ * {2}([^ *] *)\\ * {2}
"The Fox has ran **around** the corner."
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;
"The fox has ran *around the corner*
\\*{2}([^*]*)\\*{2}