Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
529 views
in Technique[技术] by (71.8m points)

ios - Troubles with changing the font size in UITableViewHeaderFooterView

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...