Một câu hỏi nhanh chóng, cho một câu trả lời nhanh chóng (vì tôi không tìm thấy bất kỳ):UITableView titleForSection phông chữ
Có cách nào để thay đổi font chữ của tiêu đề của phần (do titleForSection) trong iPhone?
Cảm ơn rất nhiều!
Một câu hỏi nhanh chóng, cho một câu trả lời nhanh chóng (vì tôi không tìm thấy bất kỳ):UITableView titleForSection phông chữ
Có cách nào để thay đổi font chữ của tiêu đề của phần (do titleForSection) trong iPhone?
Cảm ơn rất nhiều!
Bạn sẽ phải sử dụng phương thức viewForHeaderInSection:
và cung cấp chế độ xem của riêng bạn. May mắn thay đây có thể là một UILabel với một phông chữ được chỉ định, vì vậy bạn có thể làm điều đó khá dễ dàng.
Cảm ơn Jasarien! Bạn đã hoàn toàn đúng.
tôi để lại mã của tôi ở đây để giúp đỡ người khác với cùng một vấn đề:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *sectionTitle = @"Just a title";
// Create label with section title
UILabel *label = [[[UILabel alloc] init] autorelease];
label.frame = CGRectMake(0, 0, 284, 23);
label.textColor = [UIColor blackColor];
label.font = [UIFont fontWithName:@"Helvetica" size:14];
label.text = sectionTitle;
label.backgroundColor = [UIColor clearColor];
// Create header view and add label as a subview
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
[view autorelease];
[view addSubview:label];
return view;
}
Bạn cần phải ghi đè
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
để cung cấp cho chiều cao cần thiết cho việc tiêu đề phần. Khác nó sẽ chồng chéo lên ô.
viewForHeaderInSection có thể làm việc tốt ... nhưng đây là một sự thay thế mà hoạt động tốt đối với tôi (Xcode 5 và mục tiêu iOS7):
// To set the background color and text color of the Table Headers
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
// Background color
view.tintColor = [UIColor colorWithRed:0.329 green:0.557 blue:0.827 alpha:1.000];
// Text Color & Alignment
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
[header.textLabel setTextColor:[UIColor whiteColor]];
[header.textLabel setTextAlignment:NSTextAlignmentCenter];
// Text Font
UIFont *saveFont = header.textLabel.font;
[header.textLabel setFont:[UIFont fontWithName:saveFont.fontName size:18.0]];
// Another way to set the background color
// Note: does not preserve gradient effect of original heade!r
// header.contentView.backgroundColor = [UIColor blackColor];
}
này là rất tốt, tuy nhiên, UILabel là một lớp con trực tiếp của UIView, vì vậy bạn không thực sự cần tạo chế độ xem 'tiêu đề' và thêm nhãn làm chế độ xem phụ. Bạn có thể chỉ cần trả lại chính nhãn đó làm chế độ xem tiêu đề. – Jasarien
Nếu bạn trả lại chính nhãn làm tiêu đề, bạn sẽ cho nó một số bù đắp ở bên trái như thế nào? Thay đổi origin.x của khung nhãn trong trường hợp đó không hoạt động .. – talkol
Chỉ dòng tiêu đề đơn) –