简体   繁体   English

如何在NSString中获取特定字符的所有NSRange?

[英]How to get all NSRange of a particular character in a NSString?

I have two NSStrings: orgText and searchLetter . 我有两个NSStrings: orgTextsearchLetter
I want to highlight every occurrences of the searchLetter in the orgText with a red color. 我想在该searchLetter的每一个出现的突出orgText为红色。
How can I get the NSRange of all occurrences of the searchLetter ? 我怎样才能获得NSRange中所有出现的searchLetter
for eg : 例如:

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

I want to hightlight all "a" occurrences in "abcahaiapaoiuiapplma" with red color. 我想高亮显示红色“abcahaiapaoiuiapplma”中的所有“a”事件。
Thanks. 谢谢。

I wrote this method for my project - SUITextView with highlight : 我为我的项目编写了这个方法 - 带有高亮显示的SUITextView

- (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;
}

And in my category of NSMutableAttributedString: 在我的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];
}

I'm not seeing any solution with regular expression, so I've created an elegant one, it may be useful for someone in the future. 我没有看到任何正则表达式的解决方案,所以我创建了一个优雅的解决方案,它可能对将来的某个人有用。

- (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;
    }
}

Code crash at "setTextColor" for MutableAttributeString MutableAttributeString的“setTextColor”代码崩溃

instead of it use below code 而不是使用下面的代码

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

this is an easier way of doing it 这是一种更简单的方法

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM