UITableView titleForHeaderInSection shows all caps

前端 未结 14 919
孤独总比滥情好
孤独总比滥情好 2021-01-30 16:28

I am using titleForHeaderInSection to show a header for a UITableView section. It worked fine with the iOS6 SDK, but the iOS7 SDK shows the header in all CAPS.

I guess

相关标签:
14条回答
  • 2021-01-30 17:00

    solution which i found is add Title in "titleForHeaderInSection" method

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
       return @"Table Title";
    }
    

    and then call the willDisplayHeaderView method to Update :

    - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section 
    {
        UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
        header.textLabel.textColor = [UIColor darkGrayColor];
        header.textLabel.font = [UIFont boldSystemFontOfSize:18];
        CGRect headerFrame = header.frame;
        header.textLabel.frame = headerFrame;
        header.textLabel.text= @"Table Title";
        header.textLabel.textAlignment = NSTextAlignmentLeft;
    }
    
    0 讨论(0)
  • 2021-01-30 17:02

    In addition to @Animal451's post. For swift 3 you can use

    func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
      guard section == 0 ,
        let tableViewHeaderFooterView = view as? UITableViewHeaderFooterView
        else { return }
    
      tableViewHeaderFooterView.textLabel?.text = "Your awesome string"
     }
    

    And then ignore - titleForHeaderInSection:

    Keep in mind that this code is for 1st section only. If you want to go through all of your sections, you'll need to add support for them

    0 讨论(0)
  • 2021-01-30 17:03

    In Swift,

    override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        let headerView = view as! UITableViewHeaderFooterView
        headerView.textLabel.text = "My_String"
    }
    
    0 讨论(0)
  • 2021-01-30 17:03

    Simplest Solution for was below: Swift 2.x

    func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    
            header.textLabel?.text = header.textLabel?.text?.capitalizedString
    }
    
    0 讨论(0)
  • 2021-01-30 17:07

    With RubyMotion / RedPotion, paste this into your TableScreen:

      def tableView(_, willDisplayHeaderView: view, forSection: section)
        view.textLabel.text = self.tableView(_, titleForHeaderInSection: section)
      end
    

    I think what's happening is that somewhere in there the text gets set to ALL CAPS. This little trick RESETS the text back to whatever it was originally. Works like a charm for me!

    0 讨论(0)
  • 2021-01-30 17:10

    One solution I found is to utilize UITableViewHeaderFooterView.

    Instead of

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
        return @"some title";
    }
    

    Do

    - (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        static NSString *identifier = @"defaultHeader";
        UITableViewHeaderFooterView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:identifier];
        if (!headerView) {
            headerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:identifier];
        }
        headerView.textLabel.text = @"some title";
        return headerView;
    }
    

    The annoying downside is that the table view will no longer automatically adjust the section header height for you. So if your header heights varies, you'll have to do something like this:

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        id headerAppearance = [UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil];
        UIFont *font = [headerAppearance font];
        CGFloat viewWidth = CGRectGetWidth(tableView.frame);
        CGFloat xInset = 10;
        CGFloat yInset = 5;
        CGFloat labelWidth = viewWidth - xInset * 2;
        CGSize size = [sectionInfo.name sizeWithFont:font constrainedToSize:CGSizeMake(labelWidth, MAXFLOAT)];
        return size.height + yInset * 2;
    }
    

    I really don't like hard-coding layout information (the inset) this way, as it might break in the future version. If anyone has a better solution to get/set the header height, I'm all ears.

    0 讨论(0)
提交回复
热议问题