如何在引号之间找到所有字符串的所有范围

时间:2014-07-15 09:57:48

标签: ios objective-c nsattributedstring nsrange

我已经在这个问题上工作了几天但却无法找到解决方案。我有一个非常长的字符串,其中包含很多引号,我希望能够找到所有字符串(此字符串的范围),以便我可以使用NSMutableAttributedString

来加粗它们

实施例 这是一个字符串"带引号"文字,还有一些"更多"引用文字。 我希望能够将此字符串转换为以下内容: 这是一个字符串"带引号" 文字,还有"更多" 引用的文字。

这是我到目前为止所做的,但它不会完成剩余的字符串:

    - (void)stringBetweenString:(NSString*)start andString:(NSString*)end inString:(NSMutableAttributedString *)text
     {
          NSRange startRange = [[text string] rangeOfString:start];
          if (startRange.location != NSNotFound)
          {
              NSRange targetRange;
              targetRange.location = startRange.location + startRange.length;
              targetRange.length = [text length] - targetRange.location;
              NSRange endRange = [[text string] rangeOfString:end options:0 range:targetRange];
              if (endRange.location != NSNotFound)
              {
                  targetRange.length = endRange.location - targetRange.location;
                 [text addAttribute:NSFontAttributeName value:[NSFont fontWithName:@"Helvetica-Bold" size:12.0] range:targetRange];
              }
          }
      }

1 个答案:

答案 0 :(得分:1)

如果文字太长,这可能会有点慢,但它有效。使用Regex解决方案。

NSString *string = @"Example This is a string \"with quoted1\" text and there is \"some more1\" quoted text. I want to be able to turn this string into the following: This is a string \"with quoted2\" text and there is \"some more2\" quoted text.";

NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:string attributes:nil];

int leftFromLeft = 0;

while ([string rangeOfString:@"\""].location != NSNotFound) {

    NSRange quoteLocationFirst  = [string
                                   rangeOfString:@"\""
                                   options:0
                                   range:NSMakeRange(leftFromLeft, string.length - leftFromLeft)
                                   ];

    leftFromLeft = quoteLocationFirst.location + quoteLocationFirst.length;

    NSRange quoteLocationSecond = [string
                                   rangeOfString:@"\""
                                   options:0
                                   range:NSMakeRange(leftFromLeft, string.length - leftFromLeft)
                                   ];

    NSRange quotedTextRange = NSMakeRange(
                                  quoteLocationFirst.location,
                                  quoteLocationSecond.location - quoteLocationFirst.location + 1
                                  );

    UIFont *font = [UIFont fontWithName:@"Helvetica-Bold" size:30.0f];
    [attString addAttribute:NSFontAttributeName value:font range:quotedTextRange];

    NSLog(@"%@ \r\n\r\n", [string substringWithRange:quotedTextRange]);

    leftFromLeft = quoteLocationSecond.location + quoteLocationSecond.length;

    if ([string rangeOfString:@"\"" options:0 range:NSMakeRange(leftFromLeft, string.length - leftFromLeft)].location == NSNotFound) {
        string = @"";
    }
}

修改

正则表达式解决方案似乎更好/更快。

NSString *string = @"Example This is a string \"with quoted1\" text and there is \"some more1\" quoted text. I want to be able to turn this string into the following: This is a string \"with quoted2\" text and there is \"some more2\" quoted text. Example This is a string \"with quoted3\" text and there is \"some more3\" quoted text. I want to be able to turn this string into the following: This is a string \"with quoted4\" text and there is \"some more4\" quoted text.";

NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:string attributes:nil];

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\"([^\"]*)\"" options:NSRegularExpressionCaseInsensitive error:nil];

NSArray *arrayOfAllMatches = [regex matchesInString:string options:0 range:NSMakeRange(0, string.length)];

for (NSTextCheckingResult *match in arrayOfAllMatches) {

    UIFont *font = [UIFont fontWithName:@"Helvetica-Bold" size:30.0f];
    [attString addAttribute:NSFontAttributeName value:font range:match.range];

    //NSLog(@"%@", [string substringWithRange:match.range]);
}