我正在尝试创建一个NSMutableArray
从中发现的范围NSRegularExpression
,但我无法获得NSMutableArray
保存对象。帮助?
通过以下方式声明数组:NSMutableArray *matches = [[NSMutableArray alloc]init];
在我的正则表达式循环结束时:
for (NSTextCheckingResult *aMatch in minedMatches) {
NSRange matchRange = [aMatch range];
[matches addObject: [NSValue valueWithRange:matchRange]];
}
在我的代码的另一部分,我有一个想要使用的 for 循环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
在一个单独的位置使用,因为我只需要更改“Go”和“end”等关键词部分之间的文本颜色。
**编辑 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]
,这是一个全局对象。
目前,如果用户输入“Start the cat is outside end”,则当用户输入“end”时,“cat”和“outside”两个词会改变颜色。如果用户输入更多字符串,例如:“Start the cat is now inside end”,则“cat”这个词会在用户输入“end”之前自动变为绿色。我想防止这种情况发生。我只希望在“开始......结束”的各个部分改变颜色
所有外部变量都处于工作状态,到目前为止我唯一无法得到的是addAttribute
from the array of range inmatches
因为虽然它没有说它是nil
,但在条件matches.count
中是 0 。else if()