Changing Font Size For UITableView Section Headers

前端 未结 11 785
借酒劲吻你
借酒劲吻你 2020-12-12 09:28

Can someone please instruct me on the easiest way to change the font size for the text in a UITableView section header?

I have the section titles implemented using t

11条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-12 10:04

    With this method you can set font size, font style and Header background also. there are have 2 method for this

    First Method

    - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section{
            UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
            header.backgroundView.backgroundColor = [UIColor darkGrayColor];
            header.textLabel.font=[UIFont fontWithName:@"Open Sans-Regular" size:12];
            [header.textLabel setTextColor:[UIColor whiteColor]];
        }
    

    Second Method

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
        UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 30)];
    //    myLabel.frame = CGRectMake(20, 8, 320, 20);
        myLabel.font = [UIFont fontWithName:@"Open Sans-Regular" size:12];
        myLabel.text = [NSString stringWithFormat:@"   %@",[self tableView:FilterSearchTable titleForHeaderInSection:section]];
    
        myLabel.backgroundColor=[UIColor blueColor];
        myLabel.textColor=[UIColor whiteColor];
        UIView *headerView = [[UIView alloc] init];
        [headerView addSubview:myLabel];
        return headerView;
    }
    

提交回复
热议问题