2014-03-14 74 views
1

我是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; 
} 

回答

0

您可以使用它的backgroundColor enter code here財產

label.backgroundColor = [UIColor redColor]; 

把只分配背景顏色UILabel,經過初始化標籤

1

,如果你想申請上的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]]; 
} 



}