2013-10-01 91 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

相关问题