How to change font color of the title in grouped type UITableView?

前端 未结 6 1573
轮回少年
轮回少年 2020-12-13 03:55

I have a grouped type table view and it looks pretty cool.

But, if I change the background color of the table to black, the titles becomes unclear.

Is it pos

相关标签:
6条回答
  • 2020-12-13 04:18

    Yes... It works great now!

    I created tableView:viewForHeaderInSection: method and created a UIView

    UIView *customTitleView = [ [UIView alloc] initWithFrame:CGRectMake(10, 0, 300, 44)];
    

    Then i created a UILabel & set the text values & colors to the label. Then i added the label to the view

    UILabel *titleLabel = [ [UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 44)];
    titleLabel.text = @"<Title string here>";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.backgroundColor = [UIColor clearColor];
    [customTitleView addSubview:titleLabel];
    

    So my tableView:viewForHeaderInSection: method looks like...

    -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
        UIView *customTitleView = [ [UIView alloc] initWithFrame:CGRectMake(10, 0, 300, 44)];
        UILabel *titleLabel = [ [UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 44)];
        titleLabel.text = @"<Title string here>";
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.backgroundColor = [UIColor clearColor];
        [customTitleView addSubview:titleLabel];
        return customTitleView;
    }
    

    We should add tableView:heightForHeaderInSection: method for providing some space to the title.

    -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
    {
        return 44; 
    }
    
    0 讨论(0)
  • 2020-12-13 04:23

    If you just need to change the color or font on the header, use tableView: willDisplayHeaderView: forSection:. Here is an example in swift:

    Swift v5:

    override public func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    
        if let view = view as? UITableViewHeaderFooterView {
            view.backgroundView?.backgroundColor = UIColor.blue
            view.textLabel?.backgroundColor = UIColor.clear
            view.textLabel?.textColor = UIColor.white
        }
    }
    

    Original:

    override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    
        if let view = view as? UITableViewHeaderFooterView {
            view.backgroundView?.backgroundColor = ThemeBlue
            view.textLabel.backgroundColor = UIColor.clearColor()
            view.textLabel.textColor = UIColor.whiteColor()
        }
    
    }
    
    0 讨论(0)
  • 2020-12-13 04:29

    From apple documentation

    The table view uses a fixed font style for section header titles. If you want a different font style, return a custom view (for example, a UILabel object) in the delegate method tableView:viewForHeaderInSection: instead.

    So use the below method and return your custom view (UILabel) with your choice of font.

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

    Read from apple documentation

    0 讨论(0)
  • 2020-12-13 04:35

    To use the default coordinates and the sections in TableView, with white-color font and shadow:

    -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
        if (sectionTitle == nil) {
            return nil;
        }
    
        UILabel *label = [[UILabel alloc] init];
        label.frame = CGRectMake(20, 8, 320, 20);
        label.backgroundColor = [UIColor clearColor];
        label.textColor = [UIColor whiteColor];
        label.shadowColor = [UIColor grayColor];
        label.shadowOffset = CGSizeMake(-1.0, 1.0);
        label.font = [UIFont boldSystemFontOfSize:16];
        label.text = sectionTitle;
    
        UIView *view = [[UIView alloc] init];
        [view addSubview:label];
    
        return view;
    }
    
    0 讨论(0)
  • 2020-12-13 04:35
    if ([UIDevice currentDevice].systemVersion.floatValue > 7.0) {
        [[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setTextColor:[UIColor whiteColor]];
    }
    
    0 讨论(0)
  • 2020-12-13 04:41

    Swift 4.2

    UILabel.appearance(whenContainedInInstancesOf: [UITableViewHeaderFooterView.self]).textColor = .white
    
    0 讨论(0)
提交回复
热议问题