根据设备大小调整UITableViewCell的字体大小

时间:2019-03-28 00:56:24

标签: swift uitableview font-size

一直在尝试根据设备大小动态更改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上,我的桌子看起来像这样:

enter image description here

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

enter image description here

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

谢谢。

编辑:我不使用情节提要;请不要建议。

有趣的是,如果我的cellForRow方法中包含以下代码:

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

我可以调整字体大小,但第一行(无)始终为空...

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

使用Size-Class并在“字体”选项卡(在情节提要中)的前面打上+符号,然后选择“常规*常规”并根据需要设置字体大小。

相关问题