iphone - 如何在 iPhone 中获取 UILabel 标签

标签 iphone ios objective-c nsmutablearray uilabel

我正在从 NSMutableArray 动态创建标签。在创建标签时,我为每个标签设置了标签。我有一个名为 wordArrayNSMutableArray。现在,我想检查我的字符串是否在 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/

相关文章:

iphone - 遇到一些我不知道的代码语法。谁能解释一下

android - iOS 4 上的共享首选项

objective-c - 如何检测应用程序是否基于文档?

ios - Swift:访问存在的属性时为 "unrecognized selector"

iphone - 删除其他框架后得到无法识别的选择器

ios - 如何在 ARkIt 2 中将图像包裹在 3d 对象上?

iphone - 如何在 UIScrollView 中进行垂直分页

ios - 为什么 [RACLifting rac_liftSelector :] print different results between case A and B

ios - 如何叠加相机捕获的图像?

ios - 如何在 UICollectionView performBatchUpdates block 中对移动、插入、删除和更新进行排序?