我的视图中需要一个水平 ScrollView,我在数组中有一些单词然后我想在 ScrollView 上显示这些单词,我该怎么做?
提前致谢。
您可以参考以下代码:
for (NSString *text in labelArray)
{
label = [[UILabel alloc] initWithFrame:CGRectMake(scrollWidth,12,132,140)];
imageView.backgroundColor=[UIColor whiteColor];
scrollWidth +=165;
label.text=text;
[bookScroll addSubview:label];
}
根据您的要求调整标签的滚动宽度和边界。
你可以做这样的事情
CGRect labelFrame = CGRectMake(0.0f, 0.0f, scrollView.frame.size.width, scrollView.frame.size.height);
for (NSString *text in wordsArray) {
UILabel *label = [[UILabel alloc]initWithFrame:labelFrame];
label.text = text;
[scrollView addSubview:label];
labelFrame.origin.x +=labelFrame.size.width;
}
CGSize contentSize = scrollView.frame.size;
contentSize.width = labelFrame.origin.x;
scrollView.contentSize = contentSize;
int pos = 5;
for (int i=0; i<[yourArray count]; i++)
{
UILabel *lbl = [[UIImageView alloc]init];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
[lbl setFrame:CGRectMake(pos, 0, 300, 440)];
[_scrollView setContentSize:CGSizeMake([yourArray count]*320, 440)];
pos = pos + 320;
}
else
{
[lbl setFrame:CGRectMake(pos, 0, 748, 1004)];
[_scrollView setContentSize:CGSizeMake([yourArray count]*768, 1004)];
pos = pos + 768;
}
lbl.text = [[yourArray objectAtIndex:i]valueForKey:@"text"]];
[_scrollView addSubview:lbl];
}
看看EasyTableView,通过一些代码修改,你可以实现你想要的。此外,在该项目中还有很多东西要学习。
至少,看看它。