我是 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/