Changing Font Size For UITableView Section Headers

前端 未结 11 764
借酒劲吻你
借酒劲吻你 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条回答
  •  攒了一身酷
    2020-12-12 10:07

    Unfortunately, you may have to override this:

    In Objective-C:

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

    In Swift:

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
    

    Try something like this:

    In Objective-C:

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
        UILabel *myLabel = [[UILabel alloc] init];
        myLabel.frame = CGRectMake(20, 8, 320, 20);
        myLabel.font = [UIFont boldSystemFontOfSize:18];
        myLabel.text = [self tableView:tableView titleForHeaderInSection:section];
    
        UIView *headerView = [[UIView alloc] init];
        [headerView addSubview:myLabel];
    
        return headerView;
    }
    

    In Swift:

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    
        let myLabel = UILabel()
        myLabel.frame = CGRect(x: 20, y: 8, width: 320, height: 20)
        myLabel.font = UIFont.boldSystemFont(ofSize: 18)
        myLabel.text = self.tableView(tableView, titleForHeaderInSection: section)
    
        let headerView = UIView()
        headerView.addSubview(myLabel)
    
        return headerView
    }
    

提交回复
热议问题