开发者

UITableView titleForSection font

A quick question, for a quick answer (since I'm not finding any):

Is there a way to change the font of the sectio开发者_运维百科n's title (given by titleForSection) in iPhone?

Thanks a lot!


Thanks Jasarien! You were absolutely right.

I leave my code here to help someone with the same problem:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

   NSString *sectionTitle = @"Just a title";

    // Create label with section title
    UILabel *label = [[[UILabel alloc] init] autorelease];
    label.frame = CGRectMake(0, 0, 284, 23);
    label.textColor = [UIColor blackColor];
    label.font = [UIFont fontWithName:@"Helvetica" size:14];
    label.text = sectionTitle;
    label.backgroundColor = [UIColor clearColor];

    // Create header view and add label as a subview
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
    [view autorelease];
    [view addSubview:label];

    return view;
}


You'll have to use the viewForHeaderInSection: method and provide your own view. Luckily this can be a UILabel with a specified font, so you can do it quite easily.


You have to override

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

to give required height for the section title. Else it will overlap with the cell.


viewForHeaderInSection might work fine... but here's an alternative that works fine for me (Xcode 5 and IOS7 target):

// To set the background color and text color of the Table Headers
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    // Background color
    view.tintColor = [UIColor colorWithRed:0.329 green:0.557 blue:0.827 alpha:1.000];

    // Text Color & Alignment
    UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
    [header.textLabel setTextColor:[UIColor whiteColor]];
    [header.textLabel setTextAlignment:NSTextAlignmentCenter];
    // Text Font
    UIFont *saveFont = header.textLabel.font;
    [header.textLabel setFont:[UIFont fontWithName:saveFont.fontName size:18.0]];

    // Another way to set the background color
    // Note: does not preserve gradient effect of original heade!r
    // header.contentView.backgroundColor = [UIColor blackColor];
}

Here's what it looks like

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜