UITableView titleForHeaderInSection shows all caps

前端 未结 14 921
孤独总比滥情好
孤独总比滥情好 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:12

    Swift 3.x:

    if let headerView = view as? UITableViewHeaderFooterView {
        headerView.textLabel?.text? = headerView.textLabel?.text?.capitalized ?? ""
    }
    
    0 讨论(0)
  • 2021-01-30 17:16

    Yes, we had very much a similar problem to this, and my own solution was as follows:

    The Apple UITableViewHeaderFooterView documentation (the link to it is irritatingly long but you can find it easily with your favourite search engine) says you can access the textLabel of the header view without having to format your own view via viewForHeaderInSection method.

    textLabel A primary text label for the view. (read-only)

    @property(nonatomic, readonly, retain) UILabel *textLabel Discussion Accessing the value in this property causes the view to create a default label for displaying a detail text string. If you are managing the content of the view yourself by adding subviews to the contentView property, you should not access this property.

    The label is sized to fit the content view area in the best way possible based on the size of the string. Its size is also adjusted depending on whether there is a detail text label present.

    With some additional searching the best place to modify the label text was the willDisplayHeaderView method (suggested in How to implement `viewForHeaderInSection` on iOS7 style?).

    So, the solution I came up with is pretty simple and just does a transformation of the text label string, after it's actually been set by titleForHeaderInSection:

    -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
            //I have a static list of section titles in SECTION_ARRAY for reference.
            //Obviously your own section title code handles things differently to me.
        return [SECTION_ARRAY objectAtIndex:section];
    }
    

    and then simply call the willDisplayHeaderView method to modify how it looks:

    - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
    {
        if([view isKindOfClass:[UITableViewHeaderFooterView class]]){
            UITableViewHeaderFooterView *tableViewHeaderFooterView = (UITableViewHeaderFooterView *) view;
            tableViewHeaderFooterView.textLabel.text = [tableViewHeaderFooterView.textLabel.text capitalizedString];
        }
    }
    

    You can put in your own 'if' or 'switch' clauses in there as the section number is also passed to the method, so hopefully it'll allow you to show your user/client name in capitalised words selectively.

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

    SWIFT

    In implementation, i found out that you need to specify section header text in both titleForHeaderInSection & willDisplayHeaderView function, othervise header hides.

        extension ViewController: UITableViewDelegate, UITableViewDataSource {
    
            func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
                let sectionHeader =  datasource[section].sectionHeader  
    
                // Header Title
                return sectionHeader 
            }
    
            func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    
                guard let header = view as? UITableViewHeaderFooterView else { return }
                header.textLabel?.textColor = UIColor.darkGray
                header.textLabel?.font = UIFont.systemFont(ofSize: 20, weight: .semibold)
                header.textLabel?.frame = header.frame
    
                // Header Title
                header.textLabel?.text = datasource[section].sectionHeader
            }
    
        }
    
    0 讨论(0)
  • 2021-01-30 17:17

    Thanks @Animal451. This is more generic solution that would work with any type of header string.

    // We need to return the actual text so the header height gets correctly calculated
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
        return self.headerString;
    }
    
    - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
    {
        UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
        [header.textLabel setText:self.headerString];       //String in the header becomes uppercase. Workaround to avoid that.
    
    }
    

    To avoid duplication the header string can be declared anywhere else. I have done it in the -viewDidLoad method.

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

    Swift 5

    Static TableViewController & storyboard section title. The below code will work the first letter capitalized.

    override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
            let titleView = view as! UITableViewHeaderFooterView
            titleView.textLabel?.text =  titleView.textLabel?.text?.capitalized
        }
    
    0 讨论(0)
  • 2021-01-30 17:19

    One liner solution, based on @Dheeraj D answer:

    override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
      (view as? UITableViewHeaderFooterView)?.textLabel?.text = (view as? UITableViewHeaderFooterView)?.textLabel?.text?.capitalized
    }
    
    0 讨论(0)
提交回复
热议问题