I have This tableView
Here :
With the default left text alignment which i would like to change to a right text alignment . How can i do this ?
Now it looks like this :
Swift 4
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Your Header Name"
}
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
header.textLabel?.font = UIFont(name: "YourFontname", size: 14.0)
header.textLabel?.textAlignment = NSTextAlignment.right
}
You have to do this in UITableView Datasource method viewForHeaderInSection
:
Swift 2.3:
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerText = UILabel()
headerText.textColor = UIColor.lightGrayColor()
headerText.adjustsFontSizeToFitWidth = true
switch section{
case 0:
headerText.textAlignment = .Center
headerText.text = "This Header Will Be Centered"
case 1:
headerText.textAlignment = .Right
headerText.text = "This Header Will Be Aligned Right"
default:
headerText.textAlignment = .Left
headerText.text = "Default Will Be Left"
}
return headerText
}
EDIT: Modified the code above. You can use the section
argument to identify the section you want to modify.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With