【问题标题】:Troubles with changing the font size in UITableViewHeaderFooterView在 UITableViewHeaderFooterView 中更改字体大小的问题
【发布时间】:2014-02-09 08:07:25
【问题描述】:

问题来了,

我继承了一个 UITableViewHeaderFooterView 并想更改字体大小:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.textLabel.textColor = [UIColor colorWithWhite:0.8 alpha:1.0];
        //the font is not working
        self.textLabel.font = [UIFont systemFontOfSize:20];
        NSLog(@"aaa%@", self.textLabel.font);
    }
    return self;
}

颜色工作正常,但字体没有改变,所以我记录了出队:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UITableViewHeader *headerView = [self.tableView dequeueReusableHeaderFooterViewWithIdentifier:MWDrawerHeaderReuseIdentifier];
    headerView.textLabel.text = self.sectionTitles[@(section)];
    NSLog(@"bbb%@", headerView.textLabel.font);
    return headerView;
}

字体还在这里,所以我登录didLayoutsubviews

-(void)viewDidLayoutSubviews
{
    UITableViewHeaderFooterView *head = [self.tableView headerViewForSection:0];
    NSLog(@"ccc%@", head.textLabel.font);
}

字体大小神奇地变回了默认值!!!但是我没有在这之间做任何事情,如果我在viewDidLayoutSubviews 中再次更改字体大小,字体就会正确。

它让我发疯!!!

当子类化单元格时,我会更改相同的字体,而且效果很好!所以谁能告诉我发生了什么事?谢谢!

这是日志:

2014-02-09 16:02:03.339 InternWell[33359:70b] aaa<UICTFont: 0x8da4290> font-family: ".HelveticaNeueInterface-M3"; font-weight: normal; font-style: normal; font-size: 20.00pt

2014-02-09 16:02:03.339 InternWell[33359:70b] bbb<UICTFont: 0x8da4290> font-family: ".HelveticaNeueInterface-M3"; font-weight: normal; font-style: normal; font-size: 20.00pt

2014-02-09 16:02:03.341 InternWell[33359:70b] aaa<UICTFont: 0x8da4290> font-family: ".HelveticaNeueInterface-M3"; font-weight: normal; font-style: normal; font-size: 20.00pt

2014-02-09 16:02:03.342 InternWell[33359:70b] bbb<UICTFont: 0x8da4290> font-family: ".HelveticaNeueInterface-M3"; font-weight: normal; font-style: normal; font-size: 20.00pt

2014-02-09 16:02:03.343 InternWell[33359:70b] ccc<UICTFont: 0x8d22650> font-family: ".HelveticaNeueInterface-MediumP4"; font-weight: bold; font-style: normal; font-size: 14.00pt

2014-02-09 16:02:03.343 InternWell[33359:70b] ccc<UICTFont: 0x8d22650> font-family: ".HelveticaNeueInterface-MediumP4"; font-weight: bold; font-style: normal; font-size: 14.00pt

【问题讨论】:

  • 即使没有子类化 UITableViewHeaderFooterView 也会出现同样的问题,设置的字体会在视图呈现时重置。在 iOS 9.3 上检查。

标签: ios uitableview


【解决方案1】:

您可以实现 tableView:willDisplayHeaderView 并以这种方式更改字体:

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    if(section == ...)
    {
        UITableViewHeaderFooterView *headerView = (UITableViewHeaderFooterView *)view;
        NSAttributedString *headerText = ... ;
        headerView.textLabel.attributedText = headerText;
    }
}

【讨论】:

  • 比所选答案更好的解决方案。我刚刚在 Swift 中做了这个,效果很好(并且不需要子类):覆盖 func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { if let header = view as? UITableViewHeaderFooterView { header.textLabel.font = UIFont.boldSystemFontOfSize(22.0) } }
  • 我不会说这更好。您通过修改视图的布局来严重分离关注点〜在您描述视图布局的其余部分的文件之外。对我来说非常糟糕的代码气味。
【解决方案2】:

它似乎不是放置它的正确位置,但您可以在 UITableViewHeaderFooterView 子类的 layoutSubviews 方法中更改字体,它会正确应用。

- (void)layoutSubviews {
    [super layoutSubviews];

    // Font
    self.textLabel.font = [UIFont systemFontOfSize:20];
}

【讨论】:

  • UITableViewHeaderFooterView-layoutSubviews 中做坏事,包括更改textLabel 的颜色。如果它也改变字体,我不会感到惊讶。这也是我得到的解决方法,但它似乎是一个错误。
  • 必须有更好的方法来做到这一点 - layoutSubviews 应该只包含布局,它可以在单个渲染循环中调用多次,因此不建议放置任何东西除了这里的布局;我认为更好的方法是创建自定义标题标签,或更新willDisplayHeaderView 中的字体。
【解决方案3】:

似乎textLabel font 即使在 iOS 11 中也会被忽略。

另一种解决方案(Swift 4)是使用自定义标签。如果您已经有一个 UITableViewHeaderFooterView 子类,这应该不难。

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)
    }
}

【讨论】:

    【解决方案4】:

    这是 Swift 中的一个解决方案 - 这里的想法是我们有一个 UITableViewHeaderFooterView 的子类,称为 SNStockPickerTableHeaderView;它公开了一个名为configureTextLabel() 的方法,在调用该方法时,设置文本标签的字体和颜色。我们仅在设置标题后调用此方法,即来自willDisplayHeaderView,并且字体设置正确。

    // 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!
    }
    

    这是自定义的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!)
      }
    }
    

    这是结果,请参阅“热门股票”部分的标题:

                                 

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-14
      • 1970-01-01
      • 2019-02-05
      • 2017-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-30
      相关资源
      最近更新 更多