UITableView titleForSection шрифт - PullRequest
       9

UITableView titleForSection шрифт

8 голосов
/ 16 марта 2010

Быстрый вопрос, для быстрого ответа (так как я не нахожу никакого):

Есть ли способ изменить шрифт заголовка раздела (заданного titleForSection) в iPhone?

Большое спасибо!

Ответы [ 4 ]

39 голосов
/ 16 марта 2010

Спасибо, Jasarien! Вы были абсолютно правы.

Я оставляю здесь свой код, чтобы помочь кому-то с такой же проблемой:

- (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;
}
26 голосов
/ 16 марта 2010

Вам придется использовать метод viewForHeaderInSection: и предоставить свой собственный вид. К счастью, это может быть UILabel с указанным шрифтом, так что вы можете сделать это довольно легко.

5 голосов
/ 08 августа 2011

Вы должны переопределить

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

, чтобы задать необходимую высоту для заголовка раздела. В противном случае он будет перекрываться с клеткой.

3 голосов
/ 09 сентября 2014

viewForHeaderInSection может работать нормально ... но вот альтернатива, которая хорошо работает для меня (цель Xcode 5 и IOS7):

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

Вот как это выглядит

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...