在一个Label adjustFontSizeToFitWidth时,在UITab查看UILabel上设置匹配的字体大小

我在自定义tableViewCell有两个UILabel 。 一个标签具有宽度约束,并且在较小的屏幕(例如5S)上设置为adjustFontSizeToFitWidth。 当它没有特定的宽度限制时,如何让其他UILabel与第一个标签的字体大小相匹配?

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

这个答案什么时候调整.FontSizeToFitWidth或boundingRectWithSize来改变context.actualScaleFactor? 是不完整的,我需要在TableViewCell中发生这种情况。

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

 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。

 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