Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Troubles with changing the font size in UITableViewHeaderFooterView

Tags:

Here is the problem,

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

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

The color stuff works fine, but the font didn't change, so I log the dequeue:

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

The font still goes right in here, so I log in didLayoutsubviews:

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

and the font size is magically CHANGED back to the default!!! But I didn't do anything between there, and If I change the font size again in viewDidLayoutSubviews, the font becomes right.

It drives me CRAZY!!!

And I do the same font changing when subclass the cell, and it works fine! so can anyone tell me what's going on? Thanks!

Here is the log:

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 
like image 847
chliul Avatar asked Feb 09 '14 08:02

chliul


People also ask

What is the font used in Vim?

DejaVu Sans Mono; Bitstream Vera Sans Mono; and. Lucida Sans Typewriter Regular (free with many Microsoft products).


1 Answers

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;     } } 
like image 121
Apoc Avatar answered Sep 26 '22 22:09

Apoc