Troubles with changing the font size in UITableViewHeaderFooterView

前端 未结 4 629
萌比男神i
萌比男神i 2020-12-16 12:16

Here is the problem,

I subclass a UITableViewHeaderFooterView and want to change the font size:

- (id)initWithFrame:(CGRect)frame
{
    self = [super         


        
相关标签:
4条回答
  • 2020-12-16 12:24

    It doesn't seem like the right place to put it, but you can change the font in the layoutSubviews method of your UITableViewHeaderFooterView subclass and it will apply properly.

    - (void)layoutSubviews {
        [super layoutSubviews];
    
        // Font
        self.textLabel.font = [UIFont systemFontOfSize:20];
    }
    
    0 讨论(0)
  • 2020-12-16 12:32

    Here is one a solution in Swift - the idea here is that we have a subclass of UITableViewHeaderFooterView called SNStockPickerTableHeaderView; it exposes a method called, configureTextLabel() that when called, sets the font and the color of the text label. We call this method only after the title has been set, that is from, willDisplayHeaderView, and the font gets correctly set.

    // MARK: UITableViewDelegate
    
    func tableView(tableView:UITableView, willDisplayHeaderView view:UIView, forSection section:Int) {
      if let headerView:SNStockPickerTableHeaderView = view as? SNStockPickerTableHeaderView {
        headerView.configureTextLabel()
      }
    }
    
    func tableView(tableView:UITableView, viewForHeaderInSection section:Int) -> UIView? {
      var headerView:SNStockPickerTableHeaderView? = tableView.dequeueReusableHeaderFooterViewWithIdentifier(kSNStockPickerTableHeaderViewReuseIdentifier) as? SNStockPickerTableHeaderView
      if (headerView == nil) {
        headerView = SNStockPickerTableHeaderView(backgroundColor:backgroundColor,
          textColor:primaryTextColor,
          lineSeparatorColor:primaryTextColor)
      }
      return headerView!
    }
    

    And here is the custom UITableViewHeaderFooterView:

    import Foundation
    import UIKit
    
    private let kSNStockPickerTableHeaderViewLineSeparatorHeight:CGFloat = 0.5
    private let kSNStockPickerTableHeaderViewTitleFont = UIFont(name:"HelveticaNeue-Light", size:12)
    
    let kSNStockPickerTableHeaderViewReuseIdentifier:String = "stock_picker_table_view_header_reuse_identifier"
    
    class SNStockPickerTableHeaderView: UITableViewHeaderFooterView {
    
      private var lineSeparatorView:UIView?
      private var textColor:UIColor?
    
      required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
      }
    
      // We must implement this, since the designated init of the parent class
      // calls this by default!
      override init(frame:CGRect) {
        super.init(frame:frame)
      }
    
      init(backgroundColor:UIColor, textColor:UIColor, lineSeparatorColor:UIColor) {
        super.init(reuseIdentifier:kSNStockPickerTableHeaderViewReuseIdentifier)
        contentView.backgroundColor = backgroundColor
        self.textColor = textColor
        addLineSeparator(textColor)
      }
    
      // MARK: Layout
    
      override func layoutSubviews() {
        super.layoutSubviews()
        let lineSeparatorViewY = CGRectGetHeight(self.bounds) - kSNStockPickerTableHeaderViewLineSeparatorHeight
        lineSeparatorView!.frame = CGRectMake(0,
          lineSeparatorViewY,
          CGRectGetWidth(self.bounds),
          kSNStockPickerTableHeaderViewLineSeparatorHeight)
      }
    
      // MARK: Public API
    
      func configureTextLabel() {
        textLabel.textColor = textColor
        textLabel.font = kSNStockPickerTableHeaderViewTitleFont
      }
    
      // MARK: Private
    
      func addLineSeparator(lineSeparatorColor:UIColor) {
        lineSeparatorView = UIView(frame:CGRectZero)
        lineSeparatorView!.backgroundColor = lineSeparatorColor
        contentView.addSubview(lineSeparatorView!)
      }
    }
    

    Here is the result, see section header for, "Popular Stocks":

                                  enter image description here

    0 讨论(0)
  • 2020-12-16 12:33

    You could implement tableView:willDisplayHeaderView and change the font in kind of this way:

    - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
        if(section == ...)
        {
            UITableViewHeaderFooterView *headerView = (UITableViewHeaderFooterView *)view;
            NSAttributedString *headerText = ... ;
            headerView.textLabel.attributedText = headerText;
        }
    }
    
    0 讨论(0)
  • 2020-12-16 12:42

    Seems like textLabel font is ignored even in iOS 11.

    An alternative solution (Swift 4) is to use a custom label. It shouldn't be difficult if you already have a UITableViewHeaderFooterView subclass.

    class MyTableViewHeader: UITableViewHeaderFooterView {
        let customLabel = UILabel()
    
        override init(reuseIdentifier: String?) {
            super.init(reuseIdentifier: reuseIdentifier)
            setup()
        }
    
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            setup()
        }
    
        override func layoutSubviews() {
            super.layoutSubviews()
    
            customLabel.frame = contentView.bounds
        }
    
        func setup() {
            customLabel.textColor = UIColor(white: 0.8, alpha: 1)
            customLabel.font = UIFont.systemFont(ofSize: 20)
            contentView.addSubview(customLabel)
        }
    }
    
    0 讨论(0)
提交回复
热议问题