在更改UITableViewHeaderFooterView中的字体大小时遇到​​问题

这是问题,

我的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; } 

颜色的东西工作正常,但字体没有改变,所以我login出列:

 - (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; } 

字体仍然在这里,所以我logindidLayoutsubviews

 -(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子类的layoutSubviews方法中的字体,它将正确应用。

 - (void)layoutSubviews { [super layoutSubviews]; // Font self.textLabel.font = [UIFont systemFontOfSize:20]; } 

你可以实现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中的解决scheme – 这里的想法是,我们有一个名为SNStockPickerTableHeaderViewUITableViewHeaderFooterView的子类; 它公开了一个名为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!) } } 

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

在这里输入图像说明