以不同方式突出显示 TextField 中的某些文本

Highlighting some text in a TextField differently

我正在按成分创建食谱应用程序。当我在文本字段上输入成分然后按下查找食谱按钮时,它会下载包含这些输入成分的食谱,但它会在标签上显示其他成分,而我想要做的是显示带有不同颜色输入成分的标签额外的成分。

示例代码:

-(void)drawTheCell:(Recipe *)recipeObject {

    self.recipeTitle.text = recipeObject.title;

    NSArray *dividesString = [recipeObject.ingredients componentsSeparatedByString:@", "];

    NSMutableString *listIngreditents;
    for (int i=0; i < dividesString.count; i++) {


        for(int j=0; i < recipeObject.keywords.count || j < i; j++) {
        if ([dividesString objectAtIndex:i] == [recipeObject.keywords objectAtIndex:i]) {
                self.recipeIngredients.text = [dividesString objectAtIndex:i];
                self.recipeIngredients.textColor = [UIColor greenColor];

                [listIngreditents appendString:self.recipeIngredients.text];
                NSLog(@"Found a match");

            }
            else {
                    [listIngreditents appendString:[dividesString objectAtIndex:i]];
            }
    }

标签不显示成分,但显示没有成分的食谱。

您走在正确的轨道上,但是阻止您使用不同颜色的不同单词的一个主要问题是您没有使用 NSMutableAttributedString。每次你想让一个词有不同的颜色时,你需要将该属性添加到字符串的那个部分。

获得格式化的属性字符串后,您需要设置 myTextField.attributedText

除了 Fennelouski 提到的事实之外,您没有使用属性字符串,您的逻辑还有很多问题。

你当前的内循环可能不会做你期望的事情...基本上,内循环只会在 recipeObject 索引小于当前 [=13 时执行=] 索引或当前 dividesString 索引小于关键字总数,一般来说,按照这种逻辑,您基本上已经创建了对您要完成的目标毫无意义的依赖关系。

相反,您要做的是查看当前 dividesString 是否与文本字段中当前的任何关键字相匹配,因此如果想使用双循环对您尝试的逻辑进行编码,您可以在外循环的每次迭代中遍历 all 关键字,在内循环完成执行后进行 if-else 比较,例如:

- (void)drawTheCell:(Recipe *)recipeObject {

    self.recipeTitle.text = recipeObject.title;

    // Break up the recipe ingredients into components
    NSArray *dividesString = [recipeObject.ingredients componentsSeparatedByString:@", "];

    // Create a mutable attributed string so you can maintain
    // color attributes
    NSMutableAttributedString *listIngredients = [[NSMutableAttributedString alloc] init];

    // Loop through the ingredient components
    for (int i=0; i < dividesString.count; i++) {

        // Add a comma if it's not the first ingredient
        if (i > 0) {
             NSMutableAttributedString *commaString = [[NSMutableAttributedString alloc] initWithString:@", "];
            [listIngredients appendAttributedString:commaString];
        }

        // Create a boolean to indicate whether a keyword match
        // has been found
        bool matchFound = NO;

        // Loop through your "keywords"
        for(int j=0; j < recipeObject.keywords.count; j++) {

            // If the current ingredient matches the current keyword
            // change the matchFound boolean to true
            if ([[dividesString objectAtIndex:i] isEqualToString:[recipeObject.keywords objectAtIndex:j]]) {
                matchFound = YES;
                break;
            }
        }

        NSMutableAttributedString *attString = 
                [[NSMutableAttributedString alloc] initWithString:[dividesString objectAtIndex:i]];

        if (matchFound) {
            NSLog(@"Found a match");
            // Make the attributed string green
            [attString addAttribute:NSForegroundColorAttributeName 
                value:[UIColor greenColor] 
                range:NSMakeRange(0, attString.length)];   
            // Append the ingredient to listIngreditents
            [listIngredients appendAttributedString:attString];
        }
        else {
            NSLog(@"Match not found");
            // Append the ingredient to listIngreditents
            // with the default color
            [listIngredients appendAttributedString:attString];
        }
    }

    // Set the label, ex. self.recipeIngredients.attributedText = listIngredients;
}

但是有一个更简单的解决方案——我建议完全放弃内部循环并使用 containsObject: 来查看 recipeObject.keywords 数组是否包含当前成分,例如:

- (void)drawTheCell:(Recipe *)recipeObject {

    self.recipeTitle.text = recipeObject.title;

    // Break up the recipe ingredients into components
    NSArray *dividesString = [recipeObject.ingredients componentsSeparatedByString:@", "];

    // Create a mutable attributed string so you can maintain
    // color attributes
    NSMutableAttributedString *listIngredients = [[NSMutableAttributedString alloc] init];

    // Loop through the ingredient components
    for (int i=0; i < dividesString.count; i++) {

        // Add a comma if it's not the first ingredient
        if (i > 0) {
             NSMutableAttributedString *commaString = [[NSMutableAttributedString alloc] initWithString:@", "];
            [listIngredients appendAttributedString:commaString];
        }

        NSMutableAttributedString *attString = 
                [[NSMutableAttributedString alloc] initWithAttributedString:[dividesString objectAtIndex:i]];

        // If recipeObject.keywords contains the current
        // ingredient, add the green attributed string
        if([recipeObject.keywords containsObject:[dividesString objectAtIndex:i]]) {
            NSLog(@"Found a match");
            // Make the attributed string green
            [attString addAttribute:NSForegroundColorAttributeName 
                value:[UIColor greenColor] 
                range:NSMakeRange(0, attString.length)];   
            // Append the ingredient to listIngreditents
            [listIngredients appendAttributedString:attString];
        }
        // Else, simply add the string
        else {
            NSLog(@"Match not found");
            // Append the ingredient to listIngreditents
            // with the default color
            [listIngredients appendAttributedString:attString];
        }
    }

    // Set the label, ex. self.recipeIngredients.attributedText = listIngredients;
}