【问题标题】:Change TableView Header Text Color更改 TableView 标题文本颜色
【发布时间】:2012-07-02 17:36:49
【问题描述】:

我想在我的 tableView 中做一个简单的更改。我有一个自定义表格视图背景,我想更改页眉和页脚 TEXT 颜色,

仅文本颜色。

我在互联网上看到,通常我们必须使用委托方法为表格视图提供一个全新的视图,但我只会更改文本颜色(以及,如果可能的话,阴影)...

有没有一种简单的方法来做到这一点避免创建一个新的整个视图

请帮帮我。

谢谢

【问题讨论】:

    标签: iphone ios cocoa-touch


    【解决方案1】:

    如果您希望以自定义字符串以外的任何方式自定义页眉/页脚,则需要创建一个 UIView。

    这在UITableViewDataSource 文档中有记录:

    tableView:titleForHeaderInSection:

    讨论 表格视图对节标题标题使用固定的字体样式。如果您想要不同的字体样式,请在委托方法 tableView:viewForHeaderInSection: 中返回自定义视图(例如,UILabel 对象)。

    【讨论】:

    • 1)您的链接已过时,2)文档仍然包含此文本,但实际上您无需编写自定义“headerFooterView”即可更改标题
    【解决方案2】:

    抱歉,自定义标题字体颜色的唯一方法是在委托方法中创建 UIView

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

    如果您查看here 发布的答案,那么您会看到一种非常简单的方法来将其实现到您的应用程序中。您所要做的就是将该函数复制并粘贴到表视图的数据源中,然后将 UILabel 属性更改为您想要的任何内容。

    这是该帖子中的代码以方便参考:

    Originally posted by Harsh:
    
    -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        UIView *tempView=[[UIView alloc]initWithFrame:CGRectMake(0,200,300,244)];
        tempView.backgroundColor=[UIColor clearColor];
    
        UILabel *tempLabel=[[UILabel alloc]initWithFrame:CGRectMake(15,0,300,44)];
        tempLabel.backgroundColor=[UIColor clearColor]; 
        tempLabel.shadowColor = [UIColor blackColor];
        tempLabel.shadowOffset = CGSizeMake(0,2);
        tempLabel.textColor = [UIColor redColor]; //here u can change the text color of header
        tempLabel.font = [UIFont fontWithName:@"Helvetica" size:fontSizeForHeaders];
        tempLabel.font = [UIFont boldSystemFontOfSize:fontSizeForHeaders];
            tempLabel.text=@"Header Text";
    
        [tempView addSubview:tempLabel];
    
        [tempLabel release];
        return tempView;
    }
    

    【讨论】:

      【解决方案3】:

      鉴于如果您在没有UIView 子类的情况下设置页眉/页脚,则您传递的是NSString,目前无法在不创建UIView 的情况下完成您想要的操作。

      【讨论】:

      • 太糟糕了,不能使用 NSAttributedString。
      【解决方案4】:
      func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
          let headerView = view as! UITableViewHeaderFooterView
          headerView.textLabel?.textColor = ...
      }
      

      我什至可以为这个标签设置属性文本

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-11
        • 1970-01-01
        • 1970-01-01
        • 2014-12-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多