Troubles with changing the font size in UITableViewHeaderFooterView

前端 未结 4 632
萌比男神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: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

提交回复
热议问题