OStack程序员社区-中国程序员成长平台

标题: ios - 以不同方式突出显示 TextField 中的某些文本 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 05:23
标题: ios - 以不同方式突出显示 TextField 中的某些文本

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

示例代码:

-(void)drawTheCellRecipe *)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]];
            }
    }

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



Best Answer-推荐答案


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

你当前的内部循环可能不会做你期望它做的事情......基本上,内部循环只会在 recipeObject 索引小于当前 时执行>dividesString 索引或当前 dividesString 索引小于关键字的总数,一般来说,使用这种逻辑,您基本上创建了对您的行为毫无意义的依赖项试图完成。

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

- (void)drawTheCellRecipe *)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)drawTheCellRecipe *)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;
}

关于ios - 以不同方式突出显示 TextField 中的某些文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28512047/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4