有人能告诉我更改UITableView节标题中文本字体大小的最简单方法吗?

我使用以下方法实现了部分标题:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

然后,我了解了如何使用此方法成功更改节头高度:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

我使用此方法填充了UITableView单元格:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

然而,我被困在如何实际增加节标题文本的字体大小(或者就这一点而言增加字体样式)的问题上.

有人能帮忙吗?谢谢.

推荐答案

不幸的是,您可能必须覆盖以下内容:

在目标C中:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

在SWIFT中:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?

试着这样做:

在目标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;
}

在SWIFT中:

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
}

Ios相关问答推荐

更新@PersistableEnum时是否需要执行迁移块?

Nomb iOS 17.4.0中崩溃

在SWIFT中将圆角+透明背景从导入视频(.mp4)添加到导出视频(.mov)

如果没有等待,SWIFT Async让任务抛出取消异常

如何在KMM项目中处理PHPickerViewController的回调?

发生错误.请稍后重试.在 App Store 连接中

iOS 如何使用 Alamofire 解析 JSON 可编码数组

Flutter iOS 构建失败并在 ios/Runner/AppDelegate.swift 中出现多个错误

SwiftUI Disclosure Group 文本对齐

无法为 Flutter 项目构建 ipa [CocoaPods 找不到 pod "GoogleAppMeasurement" 的兼容版本:]

.overlay 和 body 是什么 swift 概念

在 Swift 中为 UITextField/UIView 摇动动画

如何获取 UICollectionViewCell 的矩形?

向标签栏控制器添加新标签

在 iPhone 中将 UIViewController 显示为弹出窗口

如何在上传到服务器之前在 iOS 上压缩/调整图像大小?

使用 Xcode 4 的 iPhone 临时构建

应用程序不包含正确的测试版权利

有没有办法在应用store 之外分发 ios 应用程序?

使用 swift 3 在 UIView 上添加阴影