我试图创建从NSRegularExpression
发现的范围的NSMutableArray
,但我无法让NSMutableArray
保存对象。帮帮我?创建一个NSMangeArray的NSRange's,然后正确读取范围值
声明由阵列:NSMutableArray *matches = [[NSMutableArray alloc]init];
在我的正则表达式的端环:
for (NSTextCheckingResult *aMatch in minedMatches) {
NSRange matchRange = [aMatch range];
[matches addObject: [NSValue valueWithRange:matchRange]];
}
在我的代码另一部分,我有循环想要使用matches
的一个;然而,它不是全:
if (matches != nil) {
for (int i = 0; i < matches.count; i++) {
[attributedString addAttribute:NSForegroundColorAttributeName value: minedColor range:[[matches objectAtIndex:i]rangeValue]];
}
}
**注:
minedColor
,minedMatches
和attributedString
在整个我的代码申报正确。我在单独的位置使用addAttribute
,因为我需要仅更改关键词部分(如“开始”和“结束”)之间的文本颜色。
**编辑1(对于整个方法请求)
- (void)textViewDidChange:(UITextView *)textView {
self.notepadTextView.font = [UIFont fontWithName:@"ProximaNova-Regular" size:20]; //custom font
UIFont *normalFont = [UIFont fontWithName:@"ProximaNova-Regular" size:20];//fail-safe font for attributed string
NSString *textEntryContents = [[self notepadTextView ]text]; //declares user inputted string
[gCore processSpeechText:textEntryContents]; //internal processing
NSMutableArray *mined = [gCore getHighLightContainer]; //array with strings that need to be colored
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:textEntryContents
attributes:@{NSFontAttributeName: normalFont}]; //initialize attributed string
matches = [[NSMutableArray alloc]init]; //initialize matches
UIColor *minedColor = [UIColor colorWithRed:(126.0/255.0) green:(204.0/255.0) blue:(136.0/255.0) alpha:1.0]; //initialize color for attributed string
BOOL colorChangeDidRun = '\0'; //initialize if color was changed
if ([gCore dataMiningInProgress] == YES) { //if it is the start of a section
colorChangeDidRun = NO;
if (mined != nil){ //fail-safe
for (int i = 0; i < mined.count; i++){
NSError *regexErrorMined;
NSRegularExpression *regexMined = [NSRegularExpression regularExpressionWithPattern:[NSString stringWithFormat:@"%@",mined[i]]
options:NSRegularExpressionCaseInsensitive error:®exErrorMined];
if (!regexErrorMined) {
NSArray *minedMatches = [regexMined matchesInString:[attributedString string]
options:0
range:NSMakeRange(0, [[attributedString string] length])];
for (NSTextCheckingResult *aMatch in minedMatches) {
NSRange matchRange = [aMatch range];
[matches addObject: [NSValue valueWithRange:matchRange]]; //add range values to matches array
}
}
}
}
}
else if ([gCore dataMiningInProgress] == NO) { //if end of section
if (colorChangeDidRun == NO) { //if the color change has not happened yet
if (matches != nil) {
for (int i = 0; i < matches.count; i++) {
colorChangeDidRun = YES; //prevent color change in unnecessary spots
[attributedString addAttribute:NSForegroundColorAttributeName value: minedColor range:[[matches objectAtIndex:i]rangeValue]];
}
}
}
}
self.notepadTextView.attributedText = attributedString; //output attributed string
}
我没有最初发布整个方法,因为它需要大量的解释,因为我敢肯定,你可以看到。基本上,用户将文本输入到文本视图。如果的文字落在“开始”和“结束”之间,则该文本然后是数据开采。这些关键字信号触发器会改变[gCore dataMiningInProgress]
的值,这是一个全局对象。
目前,如果用户输入“开始猫在户外”,当用户输入“结束”时,“猫”和“外部”这两个字将会改变颜色。如果用户输入更多的字符串,例如:“开始猫现在内部结束”,即使在用户键入“结束”之前,单词“猫”也会自动变为绿色。我想防止这种情况发生。我只希望在“开始......结束”的各个部分中更改颜色
所有外部变量都处于正常工作状态,我迄今为止唯一无法获得的是addAttribute
因为尽管它不会说nil
,matches.count
在else if()
有条件。
定义“未满”。是匹配一个局部变量还是一个实例变量? – rmaddy
你有没有证实你实际上获得有效的matchRange时添加?在[matches addObject:]上放置一个调试断点,然后查看对象数是否在 – LyricalPanda
之后立即上升。另外,您确定实际运行第一个示例代码中的for循环,并将对象添加到匹配阵列? – timgcarlson