uilabel的水平滚动?

时间:2014-07-25 11:21:36

标签: ios objective-c uitableview uiscrollview uilabel

如何编写用于为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

enter image description here

4 个答案:

答案 0 :(得分:4)

您需要做的是将标签添加到scrollView并将scrollView添加到表格视图单元格内容视图中:

// 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];

    }

答案 1 :(得分:2)

我使用Autolayouts,StackView,ScrollView和Label

完成了这项工作

这是演示代码的链接

https://github.com/pramodBiz/HorizontalLabelScroll

enter image description here

答案 2 :(得分:0)

Ramesh,您是否为滚动视图设置了内容大小。即。代码中测试

答案 3 :(得分:0)

我能够修改最初由 Włodzimierz Woźniak 创建的代码库,以便我可以水平滚动 UITableViewCell 中的标签标签。可以更改 UIScrollView 作为 UITableViewCell 中子视图的编程添加,以允许 UIScrollView 成为几乎任何 UIView 类型的子视图。

以下是代码工作原理的细分...

  1. 将字符串转换为标签
  2. 自定义标签
  3. 将标签转换为图像
  4. 将图像转换为属性文本
  5. 创建文本视图
  6. 将属性文本设置为 TextView
  7. 将 TextView 作为子视图添加到 ScollView
  8. 将 ScrollView 作为子视图添加到 TableViewCell

你可以在这里找到一个可行的实现演示:

https://github.com/austinmm/WordsAsTagLabels-Example enter image description here