[UIKit] 테이블뷰 섹션 타이틀 폰트 설정

2023. 5. 5. 20:54SWIFT/UIKit

728x90
반응형

🧸 시작

변경 전  변경 후

 

 

🧸  코드

var tableViewSectionHeader = ["즐겨찾기", "내 주변 병원"]


extension HomeViewController: UITableViewDelegate, UITableViewDataSource {
    //섹션의 개수
    func numberOfSections(in tableView: UITableView) -> Int {
        return tableViewSectionHeader.count
    }
    //섹션의 타이틀 텍스트
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return tableViewSectionHeader[section]
    }
    //섹션의 타이틀 텍스트 폰트 설정
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let titleLabel = UILabel()
        titleLabel.frame = CGRect(x: 20, y: 8, width: 320, height: 30)
        titleLabel.font = UIFont.boldSystemFont(ofSize: 24)
        titleLabel.textColor = UIColor(named: "COLOR_PURPLE")
        titleLabel.text = self.tableView(tableView, titleForHeaderInSection: section)
        titleLabel.textAlignment = .left

        let headerView = UIView()
        headerView.backgroundColor = .white
        
        headerView.addSubview(titleLabel)

        return headerView
    }
    //섹션의 타이틀 높이 설정
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 50
    }
    
    ...
    
}

 

 

 

 

 

728x90
반응형