【问题标题】:How can we change the font of tableview header?我们如何更改表格视图标题的字体?
【发布时间】:2011-10-18 20:45:29
【问题描述】:

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

【问题讨论】:

    标签: iphone ios uitableview fonts textcolor


    【解决方案1】:

    添加到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:@"<name of your font>" size:24.0]; 用于其他字体

    【讨论】:

    • 感谢 xs2bush 的快速响应,现在我可以更改标题标题颜色了
    • headerLabel.textColor = [UIColor blackColor];改成你想要的颜色。
    • 不要忘记实现tableView:heightForHeaderInSection:,否则您的部分标题将不适合您的新视图。
    • 另外不要忘记在某处缓存您的视图,否则每次出现节标题时它们都会重新绘制,这可能会很昂贵。
    【解决方案2】:

    只需执行

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

    【讨论】:

      【解决方案3】:
      - (void) tableView : (UITableView*) tableView willDisplayHeaderView : (UIView*) view forSection : (NSInteger) section{
      
          [((UITableViewHeaderFooterView) *view).textLabel setFont:(UIFont...)];
      }
      

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

      【讨论】:

        【解决方案4】:
        - (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 更改为您的字体。

        【讨论】:

          【解决方案5】:
          - (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”属性。

          【讨论】:

            猜你喜欢
            • 2015-10-01
            • 1970-01-01
            • 2011-04-05
            • 1970-01-01
            • 2011-11-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-01-01
            相关资源
            最近更新 更多