我无法将指定的“**”字符对之间的任何字符加粗。例如,在这个 NSString 中:
"The Fox has ran **around** the corner."
应该读:“狐狸跑了around角落”
这是我的代码:
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*
仍会读作“狐狸跑了around角落”,当它不应该。
有任何想法吗?
也许这可以帮助你:
http://regex101.com/r/eF6pJ8
\\*{2}([^*]*)\\*{2}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)