1

I have a table view with a bunch of sections in it. I want to change the text of the sections to make it smaller and give it more character spacing.

I have overridden the following method where I change the .textLabel property of the UITableViewHeaderFooterView object:

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    if let header = view as? UITableViewHeaderFooterView {
        header.textLabel?.attributedText = header.textLabel?.text.flatMap {
            return NSAttributedString(string: $0, attributes: [
                .kern: 1.15
            ])
        }
        header.textLabel?.font = header.textLabel?.font.withSize(10)
    }
}

This works, but the label isn't wide enough for the added spacing between the characters. I tried calling .sizeToFit on both the header and the textLabel but it didn't work. I also tried changing the frame manually but that didn't work (the frame didn't update with my values for some reason.) How do I make the textLabel wider so that the text with the added character spacing can fit into it?

Illustration of the issue

Toni Sučić
  • 1,329
  • 1
  • 13
  • 20
  • How is defined your `UITableViewHeaderFooterView`? Xib? Storyboard? Did you set a constraint for the label? https://stackoverflow.com/questions/36701095/viewforheaderinsection-incorrect-width ? – Larme Oct 19 '17 at 11:12
  • @Larme UITableViewHeaderFooterView is an out of the box view provided by Apple. I also have the same issue where increasing the kerning seems to truncate the text. I think the only solution here is to make a custom view instead of using UITableViewHeaderFooterView – johnny Sep 13 '18 at 18:20
  • That's what I ended up doing as well – Toni Sučić Sep 14 '18 at 06:44

0 Answers0