How to catch special indicated **characters** in an NSString and bold what's in between?

断了今生、忘了曾经 提交于 2019-12-02 06:58:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!