【发布时间】:2011-10-01 20:28:49
【问题描述】:
有时我希望我的视图包含 5 个 UILabels,有时是 3 个,有时是 n。
UILabel 的数量取决于从网站获取的数据。
【问题讨论】:
有时我希望我的视图包含 5 个 UILabels,有时是 3 个,有时是 n。
UILabel 的数量取决于从网站获取的数据。
【问题讨论】:
您必须在代码中而不是界面构建器中制作它们
for (int i = 0; i < n; i++)
{
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(/* where you want it*/)];
label.text = @"text"; //etc...
[self.view addSubview:label];
[label release];
}
【讨论】:
一般问题的一般答案:
while (labelsToDisplay)
{
UILabel *label = [[UILabel alloc] initWithFrame:aFrame];
[label setText:@"someText"];
[aViewContainer addSubview:label];
[label release];
}
【讨论】:
NSArray *dataArray;
float xCoordinate=10.0,yCoordinate=10.0,width=100,height=40;
float ver_space=20.0;
for (int i = 0; i <dataArray.count; i++)
{
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(xCoordinate,yCoordinate,width,height)];
label.text = [dataArray objectAtIndex:i];
[self.view addSubview:label];
yCoordinate=yCoordinate+height+ver_space;
}
【讨论】:
UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(125, 12,170,20)];
lbl.text=@"IOS";
lbl.textAlignment = NSTextAlignmentCenter;
lbl.textColor = [UIColor whiteColor];
lbl.font = [UIFont fontWithName:@"AlNile" size:10.0];
lbl.backgroundColor=[[UIColor redColor]colorWithAlphaComponent:0.5f];
lbl.layer.borderColor=[UIColor blackColor].CGColor;
lbl.layer.borderWidth=1.0f;
lbl.layer.cornerRadius = 6.0f;
[self.view addSubview:lbl];
【讨论】:
UILabel *lblTitle=[[UILabel alloc]init];
[lblTitle setFrame:CGRectMake(0, 0, 100, 100)];
[lblTitle setText:@"MAK"];
[lblTitle setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:lblTitle];
-这里会动态创建UILable。 - 但属性会设置不同。
【讨论】: