iOS动态创建UILabel
有时我希望我的视图包含5个iOS动态创建UILabel,ios,dynamic,uilabel,Ios,Dynamic,Uilabel,有时我希望我的视图包含5个UILabels,有时3个,有时n UILabel的数量取决于从网站获取的数据。一般问题的一般答案: while (labelsToDisplay) { UILabel *label = [[UILabel alloc] initWithFrame:aFrame]; [label setText:@"someText"]; [aViewContainer addSubview:label]; [label release]; } 您必
UILabel
s,有时3个,有时n
UILabel的数量取决于从网站获取的数据。一般问题的一般答案:
while (labelsToDisplay)
{
UILabel *label = [[UILabel alloc] initWithFrame:aFrame];
[label setText:@"someText"];
[aViewContainer addSubview:label];
[label release];
}
您必须在代码中而不是在界面生成器中创建它们
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];
}
for(int i=0;i
创建一个文本视图
以在标签上显示文本,并创建一个NSArray
以包含数据
有关更多信息:
-此处将动态创建UILable。
-但是属性的设置会有所不同。NSArray*dataArray;
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;
}
浮动X坐标=10.0,Y坐标=10.0,宽度=100,高度=40;
浮动空间=20.0;
对于(int i=0;i Hi),我需要创建UILabel并根据我的NSMutableArray显示数据,并在UITableview中显示。我如何才能做到这一点?您可以扩展您的答案吗?OP为什么要使用您的代码?
UILabel *lblTitle=[[UILabel alloc]init];
[lblTitle setFrame:CGRectMake(0, 0, 100, 100)];
[lblTitle setText:@"MAK"];
[lblTitle setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:lblTitle];
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;
}