如何在NSString中捕获特殊指示的**字符**并加粗它们之间的内容? [英] How to catch special indicated **characters** in an NSString and bold what's in between?

查看:96
本文介绍了如何在NSString中捕获特殊指示的**字符**并加粗它们之间的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在指定的一对**字符之间加粗任何字符。例如,在这个NSString中:

 狐狸在拐角处跑了**。 

应为:狐狸跑到左右角落 p>

这是我的代码:

  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;

我遇到问题 - 这段代码仍会捕获一对*的字符,所以在这种情况下,

 狐狸已经跑到了拐角处* 

仍然会显示为狐狸已经跑到左右角落,当它不应该时。



任何想法?

解决方案

也许这可以帮到你:



http://regex101.com/r/eF6pJ8



\\ * {2}([^ *] *)\\ * {2}


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?

解决方案

Perhaps this may help you:

http://regex101.com/r/eF6pJ8

\\*{2}([^*]*)\\*{2}

这篇关于如何在NSString中捕获特殊指示的**字符**并加粗它们之间的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆