ios - uilabel的水平滚动?

标签 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 ScrollUILabel
enter image description here

最佳答案

您需要做的是将标签添加到 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/

相关文章:

ios - 如何更改渲染对象在屏幕上的放置位置,OpenGL Es 2.0 iOS

ios - 使用 "!"和不使用 "!"声明变量有什么区别?

ios - 在保持用户位置居中的同时在 MKMapView 上安装注释

ios - 标注注释 View 不可点击中心

objective-c - 绘制 UIBezierPaths 的程序?

ios - 使用用户定义的运行时属性的 UILabel 的 cornerRadius 不起作用

iphone - 如何将 nsnumber 转换为 float

ios - 动态生成按钮时应用程序移动缓慢

ios - 在 TableView 中对行重新排序后将对象保存在 UserDefaults 中

uitableview - 为什么事件指示器显示两次