当一个标签 adjustFontSizeToFitWidth 时,在 UITableViewCell 中的 UILabels 上设置匹配的字体大小 [英] Set matching font sizes on UILabels in a UITableViewCell when one Label adjustFontSizeToFitWidth

查看:20
本文介绍了当一个标签 adjustFontSizeToFitWidth 时,在 UITableViewCell 中的 UILabels 上设置匹配的字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在自定义 tableViewCell 中有两个 UILabel.一个标签具有宽度限制,并在较小的屏幕上设置为 adjustFontSizeToFitWidth,例如5S.当它没有特定的宽度限制时,如何让另一个 UILabel 匹配第一个标签的字体大小?

I have two UILabel in a custom tableViewCell. One label has width constraints and is set to adjustFontSizeToFitWidth on smaller screens e.g. 5S. How can I get the other UILabel to match the font size of the first label, when it does not have particular width constraints?

似乎 sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode: 在 iOS7 中被弃用了,那么 Swift 的解决方案是什么?

It seems sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode: was deprecated in iOS7, so what is the Swift solution?

这个答案 何时做 adjustsFontSizeToFitWidth 或 boundingRectWithSize 改变上下文.actualScaleFactor? 不完整,我需要在 TableViewCell 中实现这一点.

This answer When do adjustsFontSizeToFitWidth or boundingRectWithSize change the context.actualScaleFactor? isn't complete, and I need this to happen in a TableViewCell.

这就是我在自定义表格视图单元格类中的内容.但它只是拾取原始尺寸而不是调整后的尺寸.

This is what I have in my custom table view cell class. But it is only picking up the original size not the adjusted size.

class CustomTVC: UITableViewCell {

@IBOutlet weak var rowTitle: UILabel!
@IBOutlet weak var rowSubtitle: UILabel!

override func awakeFromNib() {
    super.awakeFromNib()
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // makes the rowSubtitle and title have matching fonts in case of 5S
    let attributes = [NSAttributedStringKey.font: rowTitle.font]
    let textString = rowTitle.text
    let attributedString = NSMutableAttributedString(string:textString!, attributes:attributes)

    let context = NSStringDrawingContext()
    context.minimumScaleFactor = rowTitle.minimumScaleFactor
    let resultingRect = attributedString.boundingRect(with: rowTitle.bounds.size, options: .usesLineFragmentOrigin, context:context)

    print("actual context after drawing: \(context.actualScaleFactor)")

    let actualFontSize = rowTitle.font.pointSize * context.actualScaleFactor
    print("actual font size is: \(actualFontSize)")
}
}

推荐答案

我能想到的所有解决方法.希望有人会发布一个更好的方法来实际使用 adjustSizeToFitWidth.

this workaround all I can come up with. Hopefully someone will post a better way to actually use adjustSizeToFitWidth.

let ScreenWidth = UIScreen.main.bounds.size.width
    var font = UIFont()
    if ScreenWidth < 321 {
        font = UIFont.systemFont(ofSize: 16, weight: .light)
    } else {
        font = UIFont.systemFont(ofSize: 19, weight: .light)
    }
    rowTitle.font = font
    rowSubtitle.font = font

这篇关于当一个标签 adjustFontSizeToFitWidth 时,在 UITableViewCell 中的 UILabels 上设置匹配的字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆