我们如何改变tableview标题的字体?

发布于 2024-11-27 12:37:01 字数 99 浏览 2 评论 0原文

我正在为 tabelView 使用一些背景颜色,并且样式已分组。各部分标题中的文本不清楚,因此我需要修改文本颜色,以便标题文本应该可见。 我想知道我们可以更改标题文本的颜色和大小吗?

I am using some background color for the tabelView and style is grouped. The text in the header for the sections is not clear so I need to modify the the text color so that the header text should be visible.
I want to know can we change the header text's color and size?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

撕心裂肺的伤痛 2024-12-04 12:37:01

添加到 terente 的答案:

- (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:@"" size:24.0]; 对于其他字体

Adding to terente's answer:

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

You can use [UIFont fontWithName:@"<name of your font>" size:24.0]; for other fonts

九命猫 2024-12-04 12:37:01

只需实现

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

Just implement

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

And return your custom view for header.

Edit:

- (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;
}
明月夜 2024-12-04 12:37:01
- (void) tableView : (UITableView*) tableView willDisplayHeaderView : (UIView*) view forSection : (NSInteger) section{

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

您可以从其他表视图委托方法设置文本标签。

- (void) tableView : (UITableView*) tableView willDisplayHeaderView : (UIView*) view forSection : (NSInteger) section{

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

and you can set the text label from the other table view delegate method.

桃扇骨 2024-12-04 12:37:01
- (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 更改为您的字体。
- (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]];
}

Notes:

  • This will set the font to a custom font but keep the pointSize the same.
  • Also works for willDisplayFooterView.
  • Don't forget to change Your-Font-Name to your font.
眼泪都笑了 2024-12-04 12:37:01
- (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”属性。

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

We can use header.textlabel object to change other "UILabel" properties for that label.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文