【问题标题】:How to get all NSRange of a particular character in a NSString?如何在 NSString 中获取特定字符的所有 NSRange?
【发布时间】:2011-12-16 11:44:05
【问题描述】:

我有两个 NSString:orgTextsearchLetter
我想用红色突出显示 orgText 中每一次出现的 searchLetter。
如何获得所有出现的searchLetterNSRange
例如:

suppose: orgText = "abcahaiapaoiuiapplma"
         searchLetter = "a".

我想用红色突出显示“abcahaiapaoiuiapplma”中出现的所有“a”。
谢谢。

【问题讨论】:

    标签: ios objective-c nsstring nsrange


    【解决方案1】:

    我为我的项目编写了这个方法 - SUITextView with highlight:

    - (NSMutableAttributedString*) setColor:(UIColor*)color word:(NSString*)word inText:(NSMutableAttributedString*)mutableAttributedString {
    
        NSUInteger count = 0, length = [mutableAttributedString length];
        NSRange range = NSMakeRange(0, length);
    
        while(range.location != NSNotFound)
        {
            range = [[mutableAttributedString string] rangeOfString:word options:0 range:range];
            if(range.location != NSNotFound) {
                [mutableAttributedString setTextColor:color range:NSMakeRange(range.location, [word length])];
                range = NSMakeRange(range.location + range.length, length - (range.location + range.length));
                count++; 
            }
        }
    
        return mutableAttributedString;
    }
    

    在我的 NSMutableAttributedString 类别中:

    - (void) setTextColor:(UIColor*)color range:(NSRange)range {
        // kCTForegroundColorAttributeName
        [self removeAttribute:(NSString*)kCTForegroundColorAttributeName range:range]; // Work around for Apple leak
        [self addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)color.CGColor range:range];
    }
    

    【讨论】:

    • 我认为有一个额外的“}”浮动,但在 14 年仍然工作得很好
    • @dacopenhagen 你是对的。该方法是为多个单词编写的:)
    • 计数有什么意义?看看你在调试时替换了多少次?
    • 对我来说(iOS 7)它只适用于该类别方法中的其他类型的参数。 NSForegroundColorAttributeName 而不是 kCTForegroundColorAttributeName 和一个 UIColor 对象而不是它的 CGColor 属性。
    【解决方案2】:

    我没有看到任何带有正则表达式的解决方案,所以我创建了一个优雅的解决方案,它可能对将来的某人有用。

    - (BOOL)highlightString:(NSString *)string inText:(NSMutableAttributedString *)attributedString withColour:(UIColor *)color {
        NSError *_error;
        NSRegularExpression *_regexp = [NSRegularExpression regularExpressionWithPattern:string options:NSRegularExpressionCaseInsensitive error:&_error];
        if (_error == nil) {
            [_regexp enumerateMatchesInString:attributedString.string options:NSMatchingReportProgress range:NSMakeRange(0, attributedString.string.length) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
                if (result.numberOfRanges > 0) {
                    for (int i = 0; i < result.numberOfRanges; i++) {
                        [attributedString addAttribute:NSBackgroundColorAttributeName value:color range:[result rangeAtIndex:i]];
                    }
                }
            }];
            return TRUE;
        } else {
            return FALSE;
        }
    }
    

    【讨论】:

      【解决方案3】:

      MutableAttributeString 的“setTextColor”处的代码崩溃

      用下面的代码代替它

      NSDictionary *tempdict=[NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:12.0],NSFontAttributeName,color,NSForegroundColorAttributeName, nil];
      [mutableAttributedString setAttributes:tempdict range:NSMakeRange(range.location, [word length])];
      

      【讨论】:

        【解决方案4】:

        这是一种更简单的方法

        NSString *str = @"hello world";
        NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:str];
        [attr addAttributes:@{NSForegroundColorAttributeName : [UIColor redColor]} 
                      range:[str rangeOfString:@"world"]];
        

        【讨论】:

        • 注意:这只会将属性添加到第一个子字符串出现
        猜你喜欢
        • 2012-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多