Как мы можем изменить шрифт заголовка таблицы? - PullRequest
4 голосов
/ 03 августа 2011

Я использую некоторый цвет фона для tabelView и стиль сгруппирован. Текст в заголовке разделов нечеткий, поэтому мне нужно изменить цвет текста, чтобы текст заголовка был виден. Я хочу знать, можем ли мы изменить цвет и размер текста заголовка?

Ответы [ 5 ]

9 голосов
/ 03 августа 2011

Добавление к ответу Теренту:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
        UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenRect.size.width, 44.0)];
        //headerView.contentMode = UIViewContentModeScaleToFill;

        // Add the label
        UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0, -5.0, 300.0, 90.0)];
        headerLabel.backgroundColor = [UIColor clearColor];
        headerLabel.opaque = NO;
        headerLabel.text = @"Header";
        headerLabel.textColor = [UIColor blackColor];
        headerLabel.highlightedTextColor = [UIColor blackColor];

        //this is what you asked
        headerLabel.font = [UIFont boldSystemFontOfSize:17];

        headerLabel.shadowColor = [UIColor clearColor];
        headerLabel.shadowOffset = CGSizeMake(0.0, 1.0);
        headerLabel.numberOfLines = 0;
        headerLabel.textAlignment = UITextAlignmentCenter;
        [headerView addSubview: headerLabel];

        [headerLabel release];  

        // Return the headerView
        return headerView;
    }
    else return nil;
}

Вы можете использовать [UIFont fontWithName:@"<name of your font>" size:24.0]; для других шрифтов

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

Просто внедрите

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

И верните свой пользовательский вид для заголовка.

Редактировать:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIImageView *headerTitleView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, kSectionHeaderHeight)];
    [headerTitleView setImage:sectionHeaderBackgroundImage];

    UILabel *sectionTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 38) / 2, 5, 38, kSectionHeaderHeight - 10)];
    sectionTitleLabel.textColor = [UIColor redColor];
    sectionTitleLabel.backgroundColor = [UIColor clearColor];
    sectionTitleLabel.textAlignment = UITextAlignmentCenter;
    sectionTitleLabel.text = @"A";
    sectionTitleLabel.font = [UIFont fontWithName:@"yourFont" size:13];
    [sectionTitleLabel setAdjustsFontSizeToFitWidth:YES];
    [headerTitleView addSubview:sectionTitleLabel];

    return headerTitleView;
}
0 голосов
/ 28 марта 2017
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {

if ([view isKindOfClass:[UITableViewHeaderFooterView class]]) {
    UITableViewHeaderFooterView *headerView = (UITableViewHeaderFooterView*)view;
    [headerView.textLabel setFont:[UIFont fontWithName:@"Gotham Book" size:16.0f]];
}}

Мы можем использовать header.textlabel объект для изменения других свойств "UILabel" для этой метки.

0 голосов
/ 12 января 2017
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    [((UITableViewHeaderFooterView *) view).textLabel setFont:[UIFont fontWithName:@"Your-Font-Name" size:((UITableViewHeaderFooterView *) view).textLabel.font.pointSize]];
}


Примечания:

  • Это позволит установить шрифт на собственный шрифт, но оставить pointSize таким же.
  • Также работает для willDisplayFooterView.
  • Не забудьте заменить Your-Font-Name на ваш шрифт.
0 голосов
/ 28 октября 2014
- (void) tableView : (UITableView*) tableView willDisplayHeaderView : (UIView*) view forSection : (NSInteger) section{

    [((UITableViewHeaderFooterView) *view).textLabel setFont:(UIFont...)];
}

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

...