如何将字符串数组插入到 UILabel
中,并让它们加载或计数为 1?
我正在尝试将以下数组加载到 UILabel
中:
self.array = [NSArray arrayWithObjects:@"Testing 1",@"Testing 2",@"Testing 3", nil];
这里我使用的是 UIScrollView,它会在每张幻灯片之间自动更改:
for (int i=1; i<=3; i++) {
// create label
UILabel *text = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,30)];
text.text = [NSString stringWithFormat:@"%@", self.array];
text.font=[UIFont fontWithName:@"HelveticaNeueLTStd-LtCn" size:15];
text.textColor = [UIColor darkGrayColor];
text.textAlignment =NSTextAlignmentCenter;
// create imageView
UIImageView *lblView = [[UIImageView alloc] initWithFrame:CGRectMake((i-1)*scrMain.frame.size.width, 325, 280, 240)];
// set label
[lblView addSubview:text];
// apply tag to access in future
lblView.tag=i+1;
// add to scrollView
[scrMain addSubview:lblView];
}
我有一个如何使用 UIImageView
执行此操作的示例:
for (int i=1; i<=3; i++) {
// create image
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"sti%02i.png",i]];
// create imageView
UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake((i-1)*scrMain.frame.size.width + 23, 60, 280, 260)];
// set scale to fill
imgV.contentMode=UIViewContentModeScaleToFill;
// set image
[imgV setImage:image];
// apply tag to access in future
imgV.tag=i+1;
// add to scrollView
[scrMain addSubview:imgV];
}
图像在我的项目中,编号为 sti01、sti02 等等...然后它对每个图像加一,然后将它们加载到 ScrollView 中。我希望这有帮助!
最佳答案
标签的y轴位置应根据索引进行调整。
您可以使用数组计数而不是固定的“3”。
中间不需要 UIImageView。
你可以试试这个:
for (int i = 0; i < self.array.count; i++) {
// create label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, i*30, self.view.frame.size.width,30)];
label.text = [self.array objectAtIndex:i];
label.font = [UIFont fontWithName:@"HelveticaNeueLTStd-LtCn" size:15];
label.textColor = [UIColor darkGrayColor];
label.textAlignment = NSTextAlignmentCenter;
// add to scrollView
[scrMain addSubview:label];
}
关于ios - 将字符串的 NSArray 插入 UILabel 并以 1 计数。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20081644/