ios - 连续为单个标签着色

标签 ios objective-c uilabel

我是 IOS 的新手,需要一些帮助。我有几行元素,每一行都从一个数组中填充,并且这些行排列在另一行下方。该行由标签组成,标签需要不同颜色。此代码将颜色应用到整行而不是单个标签。

UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, 240)];

 int bx=0;

 for (NSString * str1 in arr1)
 {
    if ([arr1 count]<=2) {
        label=[[UILabel alloc]initWithFrame:CGRectMake(bx, -1,    scrollView.frame.size.width/[arr1 count], 100)];
        label.textAlignment=NSTextAlignmentCenter;
    }
    else
    {
        label=[[UILabel alloc]initWithFrame:CGRectMake(bx, -1, 106, 100)];
        label.textAlignment=NSTextAlignmentCenter;
    }

    [label setText:[NSString stringWithFormat:@"(%@)",str1]];
    [scrollView addSubview:label];
    bx +=label.frame.size.width;
}

最佳答案

如果你想在 UITableViewCell 上应用不同的颜色,那么, 获取用于存储不同颜色的数组,

然后在 UITABLEVIEWDATASOURCE 方法中,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// init your cell.

// use below if you have specific number of cells.
[cell.backgroundcolor = [colorarray object at index: indexpath.row]];

or you can do below if it`s suitable to you.
if([indexpath.row % 2 == 0])
{
[cell.backgroundcolor = [uicolor your color]];
}

else
{
[cell.backgroundcolor = [uicolor your different color]];
}



}

关于ios - 连续为单个标签着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22398509/

相关文章:

iphone - Sprite 突然消失 Cocos2d

ios - 如何检查 UICollectionView 是否有下一页可以显示?

objective-c - 是否可以使用 distcc 设置 Linux 机器来构建我的 XCode 项目?

ios - 我们应该对 iOS 7 应用程序进行哪些更改以支持 64 位转换?

objective-c - 类和 block 的生命周期

ios - 在 swift2 中使用 Alamofire pod 时 POST 请求不起作用?

iOS7 UISwitch 其事件 ValueChanged : Calling continuously is this Bug or what. .?

ios - 使用数组中的字符串淡入/淡出标签

ios - 缩放后为不同的标签设置相同的字体大小

ios - UIViewController 上的多个 UILabel