如何在iPhone中获取UILabel标签
我正在从如何在iPhone中获取UILabel标签,iphone,ios,objective-c,nsmutablearray,uilabel,Iphone,Ios,Objective C,Nsmutablearray,Uilabel,我正在从NSMutableArray动态创建标签。创建标签时,我为每个标签设置了标签。我有一个名为wordArray的NSMutableArray。现在,我想检查我的字符串是否在wordArray中可用, 我可以使用以下方法进行检查: [wordArray containsObject:wordStr]; 要动态创建标签,请执行以下操作: UILabel *wordLabl; int tagValue3 = 1; for (int iloop = 0; iloop < [wordArra
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;
}
UILabel*wordLabl;
int tagValue3=1;
对于(int-iloop=0;iloop<[wordArray count];iloop++)
{
WordLabel=[self-addwordLabelRect:CGRectMake(80*iloop+202020100,20)和tag:tagValue3];//30+3530*iloop+
[self.view addSubview:wordLabl];
tagValue3+=1;
}
-(UILabel*)addwordLabelRect:(CGRect)rect和tag:(int)integerValue
{
wordLabel=[[UILabel alloc]init];
wordLabel.frame=rect;
wordLabel.userInteractionEnabled=是;
wordLabel.tag=整数值;
wordLabel.backgroundColor=[UIColor clearColor];
wordLabel.font=[UIFont systemFontOfSize:15];
wordLabel.text=[NSString stringWithFormat:@“%@,[wordArray objectAtIndex:integerValue-1]];
wordLabel.textAlignment=NSTextAlignmentCenter;
wordLabel.textColor=[UIColor-whiteColor];
返回单词标签;
}
使用上面的代码,我正在创建标签和标签。
但是,如果
wordArray
包含字符串,我想更改该标签的textColor
。我想这可以通过使用标记来完成,但是我如何才能获得标签的标记值。我想你正在做类似的事情来设置标记
for (NSUInteger i = 0; i < [wordArray count]; ++i) {
UILabel * label;
// setup your label...
[label setTag:i];
[yourView addSubview:label];
}
祝你好运。我想你是在做类似的事情来设置标签吧
for (NSUInteger i = 0; i < [wordArray count]; ++i) {
UILabel * label;
// setup your label...
[label setTag:i];
[yourView addSubview:label];
}
祝你好运。如果你想从标签中获取UILabel。 您可以使用以下循环
int i=0;
for (NSObject *view in self.View.subviews)
{
if ([view isKindOfClass:[UILabel class]])
{
label = (UILabel *)[[self view] viewWithTag:wordArray[i]];
NSLog(@"%@",label.text);
//here you get your label
}
i++;
}
如果要从其标记获取UILabel。 您可以使用以下循环
int i=0;
for (NSObject *view in self.View.subviews)
{
if ([view isKindOfClass:[UILabel class]])
{
label = (UILabel *)[[self view] viewWithTag:wordArray[i]];
NSLog(@"%@",label.text);
//here you get your label
}
i++;
}
对不起,我忽略了你的代码。。。您只需在要访问相应标签的位置添加以下行:
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];
}
对不起,我忽略了你的代码。。。您只需在要访问相应标签的位置添加以下行:
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];
}
您可以将这些标签存储在一个数组中,然后根据该标签获取标签。你可以很容易地做到,我的朋友……你是否将创建的
UILabels
存储在NSMutableArray
中?显示您的代码如果您提供您的代码,将有助于提供精确的solutions@AndreyGordeev和Syed Absar我已经编辑了我的问题。你可以做的是将这些标签标记存储在一个数组中,然后根据该标记获取标签。你可以很容易地做到,我的朋友……你是否将创建的UILabels
存储在NSMutableArray
中?显示您的代码如果您提供您的代码,将有助于提供精确的solutions@AndreyGordeev和Syed Absar我已经编辑了我的问题。如果yourView
有多个具有相同标记的对象,该怎么办?哪一个被退回?如果返回的视图不是UILabel
怎么处理?如果yourView
有多个具有相同标记的对象怎么办?哪一个被退回?如果返回的视图不是一个UILabel
怎么处理?