ios - 以不同方式突出显示 TextField 中的某些文本
<p><p>我正在按成分应用创建食谱。当我在文本字段上输入成分,然后按查找食谱按钮时,它会下载带有这些输入成分的食谱,但它会在标签上显示其他成分,我想要做的是用不同颜色的输入成分显示标签附加成分。 </p>
<p>示例代码:</p>
<pre><code>-(void)drawTheCell:(Recipe *)recipeObject {
self.recipeTitle.text = recipeObject.title;
NSArray *dividesString = ;
NSMutableString *listIngreditents;
for (int i=0; i < dividesString.count; i++) {
for(int j=0; i < recipeObject.keywords.count || j < i; j++) {
if ( == ) {
self.recipeIngredients.text = ;
self.recipeIngredients.textColor = ;
;
NSLog(@"Found a match");
}
else {
];
}
}
</code></pre>
<p>标签不显示成分,但显示没有成分的食谱。 </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>除了 Fennelouski 提到的事实之外,您没有使用属性字符串,您的逻辑存在很多问题。</p>
<p>你当前的内部循环可能不会做你期望它做的事情......基本上,内部循环只会在 <code>recipeObject</code> 索引小于当前 <code> 时执行>dividesString</code> 索引或当前 <code>dividesString</code> 索引小于关键字的总数,一般来说,使用这种逻辑,您基本上创建了对您的行为毫无意义的依赖项试图完成。</p>
<p>相反,您想要做的是查看您当前的 <code>dividesString</code> 是否与当前文本字段中的任何关键字匹配,因此如果想要使用双循环对您尝试的逻辑进行编码,您可以在外循环的每次迭代期间遍历 <em>所有</em> 关键字,并在内循环完成执行后进行 if-else 比较,例如:</p>
<pre><code>- (void)drawTheCell:(Recipe *)recipeObject {
self.recipeTitle.text = recipeObject.title;
// Break up the recipe ingredients into components
NSArray *dividesString = ;
// Create a mutable attributed string so you can maintain
// color attributes
NSMutableAttributedString *listIngredients = [ 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 = [ initWithString:@", "];
;
}
// 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 ([ isEqualToString:]) {
matchFound = YES;
break;
}
}
NSMutableAttributedString *attString =
[ initWithString:];
if (matchFound) {
NSLog(@"Found a match");
// Make the attributed string green
[attString addAttribute:NSForegroundColorAttributeName
value:
range:NSMakeRange(0, attString.length)];
// Append the ingredient to listIngreditents
;
}
else {
NSLog(@"Match not found");
// Append the ingredient to listIngreditents
// with the default color
;
}
}
// Set the label, ex. self.recipeIngredients.attributedText = listIngredients;
}
</code></pre>
<p>但是有一个更简单的解决方案——我建议完全放弃内部循环并使用 <code>containsObject:</code> 来查看 <code>recipeObject.keywords</code> 数组是否包含当前成分,例如:</p>
<pre><code>- (void)drawTheCell:(Recipe *)recipeObject {
self.recipeTitle.text = recipeObject.title;
// Break up the recipe ingredients into components
NSArray *dividesString = ;
// Create a mutable attributed string so you can maintain
// color attributes
NSMutableAttributedString *listIngredients = [ 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 = [ initWithString:@", "];
;
}
NSMutableAttributedString *attString =
[ initWithAttributedString:];
// If recipeObject.keywords contains the current
// ingredient, add the green attributed string
if(]) {
NSLog(@"Found a match");
// Make the attributed string green
[attString addAttribute:NSForegroundColorAttributeName
value:
range:NSMakeRange(0, attString.length)];
// Append the ingredient to listIngreditents
;
}
// Else, simply add the string
else {
NSLog(@"Match not found");
// Append the ingredient to listIngreditents
// with the default color
;
}
}
// Set the label, ex. self.recipeIngredients.attributedText = listIngredients;
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 以不同方式突出显示 TextField 中的某些文本,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/28512047/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/28512047/
</a>
</p>
页:
[1]