So what my problem is that my text above my section is too long and gets cut off.
Any way to solve this like making it two rows long?
Any help is appreciated
So what my problem is that my text above my section is too long and gets cut off.
Any way to solve this like making it two rows long?
Any help is appreciated
You need to define the heightForHeaderInSection
and customize viewForHeaderInSection
. You can either fix all header heights at a value big enough for all lines, or calculate the required height for the specific header (as below).
let headerFont:UIFont = UIFont.systemFontOfSize(14);
let headerTexts = ["one line", "two line test123 sadfjklsadf asdjfklasjdflk asdfjklasdjfl asdfjklsadf"];
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2;
}
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return heightOfHeaderText(headerTexts[section]);
}
func heightOfHeaderText(text:String) -> CGFloat{
return NSString(string: text).boundingRectWithSize(
CGSizeMake(self.tableView.frame.size.width, 999),
options: NSStringDrawingOptions.UsesLineFragmentOrigin,
attributes: [NSFontAttributeName : headerFont],
context: nil).size.height;
}
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerLabel:UILabel = UILabel.init(frame: CGRectMake(0, 0, tableView.frame.size.width, self.tableView(tableView, heightForHeaderInSection: section)));
headerLabel.numberOfLines = 0;
headerLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping;
headerLabel.font = headerFont;
headerLabel.text = headerTexts[section];
return headerLabel;
}
Make a custom view with a label in it. And use viewForHeaderInSection delegate method to assign text to that label and return this view.
EDIT:
See this link Customize UITableView header section