【问题标题】:Adjusting font size of UITableViewCell based on the device size根据设备大小调整 UITableViewCell 的字体大小
【发布时间】:2019-08-18 16:41:12
【问题描述】:

一直在尝试根据设备大小动态更改UITableViewCell 内的字体大小。

在 UIView(在 SKScene 中)内创建表的代码:

suitsView = UIView()
suitsView.backgroundColor = .purple
suitsView.alpha = 0
self.scene?.view?.addSubview(suitsView)

suitsTableView.register(suitsTableCell.self, forCellReuseIdentifier: "cellSuits")
suitsTableView.separatorColor = UIColor(red: 153/255, green: 255/255, blue: 153/255, alpha: 1)
suitsTableView.tableFooterView = UIView()
suitsTableView.allowsMultipleSelection = true
suitsTableView.reloadData()
suitsTableView.alpha = 0
suitsTableView.populateTable()
displayText() // Displays Text

suitsTableView.rowHeight = UITableViewAutomaticDimension
suitsTableView.estimatedRowHeight = 600
suitsView.addSubview(suitsTableView)

这是我的 UITableView:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let selectedIndexPaths = tableView.indexPathsForSelectedRows
    let rowIsSelected = selectedIndexPaths != nil && selectedIndexPaths!.contains(indexPath)

    let cell : SuitsTableCell = tableView.dequeueReusableCell(withIdentifier: "cellSuits", for: indexPath as IndexPath) as! SuitsTableCell
    cell.lblName.text = self.items[indexPath.row]

    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
    return self.bounds.height/5
}

这是我的 UITableViewCell:

override init(style: UITableViewCellStyle, reuseIdentifier: String?)
{
    super.init(style: style, reuseIdentifier: reuseIdentifier)

    selectionStyle = UITableViewCellSelectionStyle.none
    backgroundColor = UIColor.yellow
    contentView.backgroundColor = UIColor.yellow

    // Font size
    var fontSize : CGFloat = 20.0

    let marginGuide = contentView.layoutMarginsGuide

    lblName = UILabel()
    lblName.textColor = UIColor.black
    lblName.font = UIFont(name:"Noteworthy-Bold", size: fontSize)

    // CONSTRAINTS
    lblName.translatesAutoresizingMaskIntoConstraints = false
    lblName.topAnchor.constraint(equalTo: marginGuide.topAnchor).isActive = true
    lblName.leadingAnchor.constraint(equalTo: marginGuide.leadingAnchor).isActive = true
    lblName.bottomAnchor.constraint(equalTo: marginGuide.bottomAnchor).isActive = true
    lblName.widthAnchor.constraint(equalToConstant: 200)
    lblName.numberOfLines = 0

    lblName.adjustsFontSizeToFitWidth = true
    lblName.contentScaleFactor = 0.5

    contentView.addSubview(lblName)
}

在 iPhone 8 上,我的桌子是这样的:

但在 iPad 上,它看起来像这样:

我需要在 iPad 上使用更大的字体。如何根据约束或行高调整字体大小?

谢谢。

编辑:我没有使用 Storyboard;请不要建议。

有趣的是,如果我的 cellForRow 方法中有这段代码:

cell.lblName.font = UIFont(name:"Noteworthy-Bold", size: (tableView.bounds.height/14).rounded())

我可以调整字体大小,但第一行(无)总是空的...

【问题讨论】:

    标签: swift uitableview font-size


    【解决方案1】:

    你可以这样使用:

     // Screen width.
        public var screenWidth: CGFloat {
             return UIScreen.main.bounds.width
        }
     // Screen height.
        public var screenHeight: CGFloat {
             return UIScreen.main.bounds.height
        }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let selectedIndexPaths = tableView.indexPathsForSelectedRows
        let rowIsSelected = selectedIndexPaths != nil && selectedIndexPaths!.contains(indexPath)
    
        let cell : SuitsTableCell = tableView.dequeueReusableCell(withIdentifier: "cellSuits", for: indexPath as IndexPath) as! SuitsTableCell
        cell.lblName.text = self.items[indexPath.row]
        cell.lblName.font = UIFont(name:"Noteworthy-Bold", size: (screenwidth * 0.0373).rounded()) // 0.0373 = requiredfontsize / 375
        return cell
    }
    

    注意:我不知道这种方法是对还是错,但我已经尝试过了,它对我有用。

    您可以通过这里了解更多信息:Scale text label by screen size

    【讨论】:

      【解决方案2】:

      使用 Size-Class 并在字体选项卡(在故事板中)之前点击 + 符号,然后选择常规 * 常规并根据您的意愿设置字体大小。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-28
        • 2012-11-11
        • 1970-01-01
        • 2012-11-01
        • 2019-02-17
        相关资源
        最近更新 更多