当一个标签 adjustFontSizeToFitWidth 时,在 UITableViewCell 中的 UILabels 上设置匹配的字体大小

编程入门 行业动态 更新时间:2024-10-27 11:24:53
本文介绍了当一个标签 adjustFontSizeToFitWidth 时,在 UITableViewCell 中的 UILabels 上设置匹配的字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我在自定义 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-29 03:15:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1183588.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字体大小   标签   UITableViewCell   adjustFontSizeToFitWidth   UILabels

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!