gpt4 book ai didi

ios - 如何在 NSString 中捕获多个特殊指示的 **characters** 并在它们之间加粗?

转载 作者:行者123 更新时间:2023-11-29 12:55:53 24 4
gpt4 key购买 nike

<分区>

我在查找由一对 ** 字符指示的多组子字符串并将它们加粗时遇到问题。例如在这个 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;

这段代码只捕获一对指定的字符,所以我得到的只是:fox has ranned around the corner

有什么想法吗?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com