我正在从 NSMutableArray
动态创建标签。在创建标签时,我为每个标签设置了标签。我有一个名为 wordArray
的 NSMutableArray
。现在,我想检查我的字符串是否在 wordArray 中可用,
我可以使用:
[wordArray containsObject:wordStr];
动态创建标签:
UILabel *wordLabl;
int tagValue3 = 1;
for (int iloop = 0; iloop < [wordArray count]; iloop++)
{
wordLabl = [self addwordLabelRect:CGRectMake(80 * iloop + 20, 420 , 100, 20)andTag:tagValue3];//30 + 35 30 * iloop+
[self.view addSubview:wordLabl];
tagValue3 += 1;
}
-(UILabel *)addwordLabelRect:(CGRect)rect andTag:(int)integerValue
{
wordLabel = [[UILabel alloc] init];
wordLabel.frame = rect;
wordLabel.userInteractionEnabled = YES;
wordLabel.tag = integerValue;
wordLabel.backgroundColor = [UIColor clearColor];
wordLabel.font = [UIFont systemFontOfSize:15];
wordLabel.text = [NSString stringWithFormat:@"%@",[wordArray objectAtIndex:integerValue - 1]];
wordLabel.textAlignment = NSTextAlignmentCenter;
wordLabel.textColor = [UIColor whiteColor];
return wordLabel;
}
使用上面的代码我正在创建标签和标签。
但是,如果 wordArray
包含我想更改该标签的 textColor
的字符串。我认为这可以使用 Tag 来完成,但是我怎样才能获得标签的标签值标签。
最佳答案
抱歉,我忽略了您的代码...您只需在要访问相应标签的位置添加以下行:
if([wordArray containsObject:wordStr])
{
UILabel *label = (UILabel *) [self.view viewWithTag:([wordArray indexOfObject:wordStr] - 1)];//since u started tag assignment from 1
label.textcolor = [UIColor yellowColor];
}
关于iphone - 如何在 iPhone 中获取 UILabel 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18459322/