【问题标题】:Horizontal scroll for uilabel?uilabel的水平滚动?
【发布时间】:2014-09-17 05:50:21
【问题描述】:

如何编写代码来为 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 ScrollUILabel

【问题讨论】:

    标签: ios objective-c uitableview uiscrollview uilabel


    【解决方案1】:

    你要做的就是将标签添加到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];
    
        }
    

    【讨论】:

      【解决方案2】:

      我已经使用 Autolayouts、StackView、ScrollView 和 Label 完成了这项工作

      这里是演示代码的链接

      https://github.com/pramodBiz/HorizontalLabelScroll

      【讨论】:

      • 你用UIStackView做什么?它不能使用简单的UIView
      • 我有使用 UIStackView 的习惯。但我认为它必须适用于简单的视图
      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        我能够修改最初由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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-10-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多