2013-10-01 88 views
1

新的iOS 7已經改變了tableview中節標題的默認字體顏色。問題是我的背景圖片使文本難以閱讀。我知道我可以改變我的背景,但我想改變所有文字瀏覽的顏色。我之前更改了應用程序委託中的uinavigationbar顏色。如果可能的話,我想使用類似於tableview的東西。我已閱讀本方法:有沒有辦法改變UITableview標題的所有實例的字體顏色

NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section]; 

if (sectionTitle == nil) { 
    return nil; 
}else{ 
    UILabel *label = [[UILabel alloc] init]; 
    label.frame = CGRectMake(20, 8, 320, 16); 
    label.textColor = [UIColor whiteColor]; 
    label.text = sectionTitle; 

    UIView *view = [[UIView alloc] init]; 
    [view addSubview:label]; 

    return view; 
} 

我這個問題是我會實現它在每個tableviewcontroller基礎。另外,當使用這個我不知道如何防止文字脫離頁面,並無法閱讀。

任何建議將是偉大的。提前致謝。

編輯:我決定添加一點澄清只是爲了顯示使用一些建議的代碼在這裏仍然是我的問題與此解決方案。 UITableView Header trails off

編輯:陪伴回答。我發現這也是需要爲頭創建多行的空間。

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ 
    if (section == ?) { 
     return 60; 
    } 
    else{ 
     return 44; 
    } 
} 

回答

2

您必須使用下面的方法來改變你的headerview:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 

UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,yourWidth,YourHeight)] ; 
headerView.backgroundColor = [UIColor colorWithRed:0.5058f green:0.6118f blue:0.8078f alpha:1.0f]; 


tableView.sectionHeaderHeight = headerView.frame.size.height; 
tableView.tableHeaderView.clipsToBounds = YES; 

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 13,320, 22)] ; 
label.text = [self tableView:tableView titleForHeaderInSection:section]; 
label.font = [UIFont boldSystemFontOfSize:16.0]; 
label.shadowOffset = CGSizeMake(0, 1); 
label.shadowColor = [UIColor grayColor]; 
label.backgroundColor = [UIColor clearColor]; 
    // Chnage your title color here 
label.textColor = [UIColor whiteColor]; 
    [label sizeToFit]; 
    label.numberOfLines = 2 ; 

[headerView addSubview:label]; 
return headerView; 
} 
+0

所以我肯定可以使用這個,但是,如果我的標題文字是什麼過長?這可能會把它關掉。我怎麼能動態地解決這個問題。假設要添加一條額外的行來容納更長的頭文件。 – jmr1706

+0

替換此行:UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(20,13,320,22)];使第三個參數長度爲320。 – astuter

+0

我的意思是很好,但我正在談論一個完整的句子或兩個頭。我確實看到此方法估計HeightForHeaderInSection,但我不完全確定這是否會有所幫助或如何使用它。 – jmr1706

相關問題