1
我遇到了一個問題,查找由一對**字符指示的多個子串,並將它們加粗。例如,在此的NSString:如何在NSString中捕獲多個特殊指示**字符**並在它們之間加粗?
The Fox has **ran** around the **corner**
應改爲:狐狸已經跑圍繞角落
這裏是我的代碼:
NSString *questionString = queryString;
NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithString:questionString];
NSRange range = [questionString rangeOfString:@"\\*{2}([^*]+)\\*{2}" options:NSRegularExpressionSearch];
if (range.location != NSNotFound) {
[mutableAttributedString setAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:size]} range:range];
}
[[mutableAttributedString mutableString] replaceOccurrencesOfString:@"**" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, queryString.length)];
return mutableAttributedString;
這個代碼只捕獲1雙所以我回來的是:狐狸已經跑到了角落
有什麼想法?
@rmaddy不,不是欺騙。我在那裏問了一個不同的問題。我實際上得到了答案並將其應用於新代碼。這個問題是一個不同的問題 – 3254523
你厭倦了在全球範圍內運行正則表達式替換嗎? – sln
這是怎麼回事?兩者都詢問如何將分隔值轉換爲屬性字符串。 – rmaddy