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

时间:2015-02-14 03:18:08

标签: ios objective-c iphone nsstring uitextfield

我正在通过原料应用创建配方。当我在文本字段上键入成分然后按下查找食谱按钮时,它会下载含有这些类型成分的食谱,但它会在标签上显示其他成分,我想要做的是显示带有不同颜色的成分的标签。其他成分。

示例代码:

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

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

2 个答案:

答案 0 :(得分:1)

除了Fennelouski提到的事实,你没有使用属性字符串,你的逻辑存在很多问题。

你当前的内部循环可能不会按预期执行...基本上,当recipeObject索引小于当前dividesString时,内部循环才会执行索引或当前dividesString索引小于关键字总数,一般来说,通过这种逻辑,您基本上创建了对您尝试完成的内容毫无意义的依赖关系。

相反,您要做的是查看当前dividesString是否与当前文本字段中的任何关键字匹配,因此如果您希望使用双循环对您尝试的逻辑进行编码,则可以循环执行在外循环的每次迭代中所有关键字,并在内循环完成执行后进行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;
}

答案 1 :(得分:0)

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

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