【问题标题】:how to catch multiple instances special indicated **characters** in an NSString and bold them in between? [duplicate]如何在 NSString 中捕获多个特殊指示的 **characters** 实例并在它们之间加粗? [复制]
【发布时间】:2014-01-14 21:06:12
【问题描述】:

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

这段代码只捕捉到一对指定的字符,所以我得到的只是:狐狸已经跑到拐角处了

有什么想法吗?

【问题讨论】:

  • @rmaddy 不,不是骗子。我在那里问了一个不同的问题。我实际上在那里得到了答案并将其应用于新代码。这个问题是另一个问题
  • 您是否厌倦了在全局上下文中运行正则表达式替换?
  • 怎么不一样?两者都询问如何将分隔值转换为属性字符串。

标签: ios objective-c regex nsregularexpression


【解决方案1】:

您必须枚举正则表达式的所有匹配项。 这有点棘手,因为当您删除限制性“**”对时,所有范围都会发生变化。

这似乎可以完成工作:

NSString *questionString = @"The Fox has **ran** around the **corner**";
NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithString:questionString];

NSError *error = nil;
NSString *pattern = @"(\\*{2})([^*]+)(\\*{2})";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];

__block NSUInteger shift = 0; // number of characters removed so far
[regex enumerateMatchesInString:questionString options:0 range:NSMakeRange(0, [questionString length])
     usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
         NSRange r1 = [result rangeAtIndex:1]; // location of first **
         NSRange r2 = [result rangeAtIndex:2]; // location of word in between
         NSRange r3 = [result rangeAtIndex:3]; // location of second **
         // Adjust locations according to the string modifications:
         r1.location -= shift;
         r2.location -= shift;
         r3.location -= shift;
         // Set attribute for the word:
         [mutableAttributedString setAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:12.0]} range:r2];
         // Remove the **'s:
         [[mutableAttributedString mutableString] deleteCharactersInRange:r3];
         [[mutableAttributedString mutableString] deleteCharactersInRange:r1];
         // Update offset:
         shift += r1.length + r3.length;
     }];

结果(在调试器控制台中):

The Fox has {
}ran{
    NSFont = "<UICTFont: 0xc03efb0> font-family: \".HelveticaNeueInterface-MediumP4\"; font-weight: bold; font-style: normal; font-size: 12.00pt";
} around the {
}corner{
    NSFont = "<UICTFont: 0xc03efb0> font-family: \".HelveticaNeueInterface-MediumP4\"; font-weight: bold; font-style: normal; font-size: 12.00pt";
}

【讨论】:

  • 谢谢!效果很好。很棒的解决方案
猜你喜欢
  • 2014-02-01
  • 1970-01-01
  • 2021-10-31
  • 2011-04-22
  • 2012-11-14
  • 1970-01-01
  • 1970-01-01
  • 2015-06-12
  • 2022-10-14
相关资源
最近更新 更多