ios – 在UITableViewHeaderFooterView中更改字体大小的麻烦

前端之家收集整理的这篇文章主要介绍了ios – 在UITableViewHeaderFooterView中更改字体大小的麻烦前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是问题,

分类一个UITableViewHeaderFooterView并想要更改字体大小:

  1. - (id)initWithFrame:(CGRect)frame
  2. {
  3. self = [super initWithFrame:frame];
  4. if (self) {
  5. // Initialization code
  6. self.textLabel.textColor = [UIColor colorWithWhite:0.8 alpha:1.0];
  7. //the font is not working
  8. self.textLabel.font = [UIFont systemFontOfSize:20];
  9. NSLog(@"aaa%@",self.textLabel.font);
  10. }
  11. return self;
  12. }

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

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

字体仍然在这里,所以我登录didLayoutsubviews:

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

并且字体大小神奇地更改回到默认值!但是我没有做任何事情,如果我在viewDidLayoutSubviews中再次更改字体大小,字体就会变得正确.

它让我疯狂!!!

而我做同样的字体改变,当子类的细胞,它的工作正常!任何人都可以告诉我发生了什么?谢谢!

这是日志:

  1. 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
  2.  
  3. 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
  4.  
  5. 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
  6.  
  7. 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
  8.  
  9. 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
  10.  
  11. 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方法中的字体,它将适用.
  1. - (void)layoutSubviews {
  2. [super layoutSubviews];
  3.  
  4. // Font
  5. self.textLabel.font = [UIFont systemFontOfSize:20];
  6. }

猜你在找的iOS相关文章