Changing Font Size For UITableView Section Headers

前端 未结 11 766
借酒劲吻你
借酒劲吻你 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: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;
    }
    
    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
  • 2020-12-12 10:09

    Swift 4 version of Leo Natan answer is

    UILabel.appearance(whenContainedInInstancesOf: [UITableViewHeaderFooterView.self]).font = UIFont.boldSystemFont(ofSize: 28)
    

    If you wanted to set a custom font you could use

    if let font = UIFont(name: "font-name", size: 12) {
        UILabel.appearance(whenContainedInInstancesOf: [UITableViewHeaderFooterView.self]).font = font
    }
    
    0 讨论(0)
  • 2020-12-12 10:10

    Swift 2.0:

    1. Replace default section header with fully customisable UILabel.

    Implement viewForHeaderInSection, like so:

      override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    
        let sectionTitle: String = self.tableView(tableView, titleForHeaderInSection: section)!
        if sectionTitle == "" {
          return nil
        }
    
        let title: UILabel = UILabel()
    
        title.text = sectionTitle
        title.textColor = UIColor(red: 0.0, green: 0.54, blue: 0.0, alpha: 0.8)
        title.backgroundColor = UIColor.clearColor()
        title.font = UIFont.boldSystemFontOfSize(15)
    
        return title
      }
    
    1. Alter the default header (retains default).

    Implement willDisplayHeaderView, like so:

      override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    
        if let view = view as? UITableViewHeaderFooterView {
          view.backgroundView?.backgroundColor = UIColor.blueColor()
          view.textLabel!.backgroundColor = UIColor.clearColor()
          view.textLabel!.textColor = UIColor.whiteColor()
          view.textLabel!.font = UIFont.boldSystemFontOfSize(15)
        }
      }
    

    Remember: If you're using static cells, the first section header is padded higher than other section headers due to the top of the UITableView; to fix this:

    Implement heightForHeaderInSection, like so:

      override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    
        return 30.0 // Or whatever height you want!
      }
    
    0 讨论(0)
  • 2020-12-12 10:10

    This is my solution with swift 5.

    To fully control the header section view, you need to use the tableView(:viewForHeaderInsection::) method in your controller, as the previous post showed. However, there is a further step: to improve performance, apple recommend not generate a new view every time but to re-use the header view, just like reuse table cell. This is by method tableView.dequeueReusableHeaderFooterView(withIdentifier: ). But the problem I had is once you start to use this re-use function, the font won't function as expected. Other things like color, alignment all fine but just font. There are some discussions but I made it work like the following.

    The problem is tableView.dequeueReusableHeaderFooterView(withIdentifier:) is not like tableView.dequeneReuseCell(:) which always returns a cell. The former will return a nil if no one available. Even if it returns a reuse header view, it is not your original class type, but a UITableHeaderFooterView. So you need to do the judgement and act according in your own code. Basically, if it is nil, get a brand new header view. If not nil, force to cast so you can control.

    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
            let reuse_header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "yourHeaderID")
            if (reuse_header == nil) {
                let new_sec_header = YourTableHeaderViewClass(reuseIdentifier:"yourHeaderID")
                new_section_header.label.text="yourHeaderString"
                //do whatever to set color. alignment, etc to the label view property
                //note: the label property here should be your custom label view. Not the build-in labelView. This way you have total control.
                return new_section_header
            }
            else {
                let new_section_header = reuse_section_header as! yourTableHeaderViewClass
                new_sec_header.label.text="yourHeaderString"
                //do whatever color, alignment, etc to the label property
                return new_sec_header}
    
        }
    
    0 讨论(0)
提交回复
热议问题