我如何编写代码来为 uilabel 数组进行水平滚动
例子:
NSArray *tagsArray = [NSJSONSerialization JSONObjectWithData:someData options:kNilOptions error:&error];
if ([tagsArray count]>0) {
for (int i=0; i<[tagsArray count]; i++) {
CGRect ansCountValueRectangle13 = CGRectMake(80*i+10,110,80,30);
UILabel *_tagsValue = [[UILabel alloc] initWithFrame:ansCountValueRectangle13];
_tagsValue.clipsToBounds=YES;
_tagsValue.tag=250;
_tagsValue.text = [tagsArray objectAtIndex:i];
_tagsValue.layer.cornerRadius=15.0;
_tagsValue.backgroundColor = [UIColor orangeColor];
_tagsValue.textAlignment = NSTextAlignmentCenter;
_tagsValue.textColor=[UIColor whiteColor];
_tagsValue.font = [UIFont boldSystemFontOfSize:16];
[testing addSubview: _tagsValue];
}
}
为此我必须添加
UIScrollView
,这些代码写在 tableViewCell
我希望每个 tableview 行都有 Horizontal Scroll
与 UILabel
最佳答案
您需要做的是将标签添加到 scrollView 并将 scrollView 添加到表格 View 单元格内容 View :
// You have to change the scroll frame to match your requirements
UIScrollView *sv = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 10.0, self.view.bounds.size.width, 35)];
sv.backgroundColor = [UIColor grayColor]; // I've added color to see when the scroll view is layout
float svWidth = 10; //Margin to content view width (feel fee to change it or remove it)
if ([tagsArray count]>0) {
for (int i=0; i<[tagsArray count]; i++) {
CGRect ansCountValueRectangle13 = CGRectMake(80*i+10,2,80,30);
UILabel *_tagsValue = [[UILabel alloc] initWithFrame:ansCountValueRectangle13];
_tagsValue.clipsToBounds=YES;
_tagsValue.tag=250;
_tagsValue.text = [tagsArray objectAtIndex:i];
_tagsValue.layer.cornerRadius=15.0;
_tagsValue.backgroundColor = [UIColor orangeColor];
_tagsValue.textAlignment = NSTextAlignmentCenter;
_tagsValue.textColor=[UIColor whiteColor];
_tagsValue.font = [UIFont boldSystemFontOfSize:16];
svWidth += 80+10;
[sv addSubview:_tagsValue];
// I don't know what is testing, you have to add the labels to scrollView
//[testing addSubview: _tagsValue];
}
// Set the content size of the scroll view, feel free to tweak it
[sv setContentSize:CGSizeMake(svWidth, 35)];
// Add scroll view to your cell, I've added it to contentView but maybe you want to add it to some subview
[cell.contentView addSubview:sv];
}
关于ios - uilabel的水平滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24954630/