UITableView 섹션 헤더에서 텍스트의 글꼴 크기를 변경하는 가장 쉬운 방법을 알려주시겠습니까?
다음 방법을 사용하여 섹션 제목을 구현했습니다.
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
그런 다음이 방법을 사용하여 섹션 헤더 높이를 성공적으로 변경하는 방법을 이해합니다.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
이 방법을 사용하여 UITableView 셀을 채웠습니다.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
그러나 섹션 헤더 텍스트의 글꼴 크기 (또는 글꼴 스타일)를 실제로 늘리는 방법에 붙어 있습니다.
누군가 도와주세요? 감사.
답변
불행히도 이것을 무시해야 할 수도 있습니다.
Objective-C에서 :
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
스위프트에서 :
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
다음과 같이 해보십시오 :
Objective-C에서 :
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UILabel *myLabel = [[UILabel alloc] init];
myLabel.frame = CGRectMake(20, 8, 320, 20);
myLabel.font = [UIFont boldSystemFontOfSize:18];
myLabel.text = [self tableView:tableView titleForHeaderInSection:section];
UIView *headerView = [[UIView alloc] init];
[headerView addSubview:myLabel];
return headerView;
}
스위프트에서 :
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let myLabel = UILabel()
myLabel.frame = CGRect(x: 20, y: 8, width: 320, height: 20)
myLabel.font = UIFont.boldSystemFont(ofSize: 18)
myLabel.text = self.tableView(tableView, titleForHeaderInSection: section)
let headerView = UIView()
headerView.addSubview(myLabel)
return headerView
}
답변
이를 수행하는 또 다른 방법은 UITableViewDelegate방법 에 응답하는 것 willDisplayHeaderView입니다. 전달 된 뷰는 실제로의 인스턴스입니다 UITableViewHeaderFooterView.
아래 예제는 글꼴을 변경하고 셀 내에서 제목 텍스트를 세로 및 가로로 가운데에 맞 춥니 다. heightForHeaderInSection테이블 뷰의 레이아웃에서 머리글 높이를 변경하도록 응답해야 합니다. (이 willDisplayHeaderView방법으로 헤더 높이를 변경하기로 결정한 경우 입니다.)
그런 다음 titleForHeaderInSection다른 섹션 제목으로 구성된이 헤더를 재사용 하기 위해 메소드에 응답 할 수 있습니다.
목표 -C
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
header.textLabel.textColor = [UIColor redColor];
header.textLabel.font = [UIFont boldSystemFontOfSize:18];
CGRect headerFrame = header.frame;
header.textLabel.frame = headerFrame;
header.textLabel.textAlignment = NSTextAlignmentCenter;
}
스위프트 1.2
(참고 : 뷰 컨트롤러가의 자손 인 경우 UITableViewController로 선언해야합니다 override func.)
override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)
{
let header:UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
header.textLabel.textColor = UIColor.redColor()
header.textLabel.font = UIFont.boldSystemFontOfSize(18)
header.textLabel.frame = header.frame
header.textLabel.textAlignment = NSTextAlignment.Center
}
스위프트 3.0
이 코드는 또한 헤더 뷰가 UITableViewHeaderFooterView 이외의 다른 앱인 경우 앱이 중단되지 않도록합니다.
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
guard let header = view as? UITableViewHeaderFooterView else { return }
header.textLabel?.textColor = UIColor.red
header.textLabel?.font = UIFont.boldSystemFont(ofSize: 18)
header.textLabel?.frame = header.frame
header.textLabel?.textAlignment = .center
}
답변
mosca1337의 답변이 올바른 해결책이지만 해당 방법에주의하십시오. 한 줄보다 긴 텍스트를 가진 헤더의 경우, tableView:heightForHeaderInSection:번거로울 수있는 헤더의 높이를 계산해야합니다 .
훨씬 선호되는 방법은 모양 API를 사용하는 것입니다.
[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setFont:[UIFont boldSystemFontOfSize:28]];
글꼴을 변경하는 동시에 높이 자체를 관리하기 위해 테이블을 그대로 둡니다.
최적의 결과를 얻으려면 테이블 뷰를 서브 클래 싱하고 포함 체인 (in appearanceWhenContainedIn:)에 추가 하여 특정 테이블 뷰에 대해서만 글꼴이 변경되도록하십시오.
답변
iOS 7에서는 이것을 사용합니다.
-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
header.textLabel.font = [UIFont boldSystemFontOfSize:10.0f];
header.textLabel.textColor = [UIColor orangeColor];
}
다음은 헤더 크기 조정 이 가능한 Swift 3.0 버전입니다.
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
if let header = view as? UITableViewHeaderFooterView {
header.textLabel!.font = UIFont.systemFont(ofSize: 24.0)
header.textLabel!.textColor = UIColor.orange
}
}
답변
스위프트 3 :
크기 만 조정하는 가장 간단한 방법 :
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header = view as! UITableViewHeaderFooterView
if let textlabel = header.textLabel {
textlabel.font = textlabel.font.withSize(15)
}
}
답변
스위프트 2.0 :
- 기본 섹션 헤더를 완전히 사용자 정의 가능한 UILabel로 바꾸십시오.
viewForHeaderInSection을 다음과 같이 구현하십시오.
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let sectionTitle: String = self.tableView(tableView, titleForHeaderInSection: section)!
if sectionTitle == "" {
return nil
}
let title: UILabel = UILabel()
title.text = sectionTitle
title.textColor = UIColor(red: 0.0, green: 0.54, blue: 0.0, alpha: 0.8)
title.backgroundColor = UIColor.clearColor()
title.font = UIFont.boldSystemFontOfSize(15)
return title
}
- 기본 헤더를 변경하십시오 (기본값 유지).
willDisplayHeaderView를 다음과 같이 구현하십시오.
override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
if let view = view as? UITableViewHeaderFooterView {
view.backgroundView?.backgroundColor = UIColor.blueColor()
view.textLabel!.backgroundColor = UIColor.clearColor()
view.textLabel!.textColor = UIColor.whiteColor()
view.textLabel!.font = UIFont.boldSystemFontOfSize(15)
}
}
알아두기 : 정적 셀을 사용하는 경우 첫 번째 섹션 헤더는 UITableView의 상단으로 인해 다른 섹션 헤더보다 높게 채워집니다. 이 문제를 해결하려면 :
heightForHeaderInSection을 다음과 같이 구현하십시오.
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 30.0 // Or whatever height you want!
}
답변
Leo Natan의 신속한 4 버전 답변 입니다
UILabel.appearance(whenContainedInInstancesOf: [UITableViewHeaderFooterView.self]).font = UIFont.boldSystemFont(ofSize: 28)
사용자 정의 글꼴을 설정하려면 사용할 수 있습니다
if let font = UIFont(name: "font-name", size: 12) {
UILabel.appearance(whenContainedInInstancesOf: [UITableViewHeaderFooterView.self]).font = font
}
