首页 > 解决方案 > 更改首选内容大小后,我的一些可重用单元格标签中的字体大小仍然不变

问题描述

我正在使用具有包含 UICollectionViewListCells 的组合布局的 UICollectionView。每个单元格包含几个 UILabel。我使用自定义字体粗细设置动态类型

extension UIFont {
    static func preferredFont(for style: TextStyle, weight: Weight) -> UIFont {
        let metrics = UIFontMetrics(forTextStyle: style)
        let desc = UIFontDescriptor.preferredFontDescriptor(withTextStyle: style)
        let font = UIFont.systemFont(ofSize: desc.pointSize, weight: weight)
        return metrics.scaledFont(for: font)
    }
}

来源:https ://mackarous.com/dev/2018/12/4/dynamic-type-at-any-font-weight

然后我更改字体大小:

在此处输入图像描述

当我滚动浏览我的 UICollectionView 时,一些单元格的字体大小保持不变,而大多数单元格的大小都被正确调整(当使用更简单的东西时,一切都很完美UIFont.preferredFont(forTextStyle: .subheadline)。响应首选内容的变化,获得正确的字体大小以呈现的最佳方法是什么?尺寸?

此外,我正在创建一个包含 SF 符号https://stackoverflow.com/a/58341241的属性字符串,并使用

let configuration = UIImage.SymbolConfiguration(textStyle: .title1)

当我更改字体大小时,图像不会动态缩放。当我重新启动应用程序时,这两个问题都消失了。是否有任何我可以在UIContentSizeCategory.didChangeNotification发布时执行的代码,或解决这两个问题的其他方法?

编辑 我能够通过使用此处列出的方法解决问题的第一部分https://spin.atomicobject.com/2018/02/02/swift-scaled-font-bold-italic/

extension UIFont {
    func withTraits(traits:UIFontDescriptor.SymbolicTraits) -> UIFont {
        let descriptor = fontDescriptor.withSymbolicTraits(traits)
        return UIFont(descriptor: descriptor!, size: 0) //size 0 means keep the size as it is
    }

    func bold() -> UIFont {
        return withTraits(traits: .traitBold)
    }

}

let boldFont = UIFont.preferredFont(forTextStyle: .headline).bold()

当首选内容大小发生变化时,我仍然坚持如何在属性字符串中获取 sf 符号 UIImage 以调整大小。

标签: iosswift

解决方案


当首选内容大小更改时,使属性字符串调整大小。

    func attributedStringDynamicTypeExample() {
        let font = UIFont(name: "Times New Roman", size: 20)!
        let fontMetrics = UIFontMetrics(forTextStyle: .title3)
        let scaledFont = fontMetrics.scaledFont(for: font)
        
        let attachment = NSTextAttachment()
        attachment.image = UIImage(named: "BigPoppa")
        attachment.adjustsImageSizeForAccessibilityContentSizeCategory = true
        
        let attributes: [NSAttributedString.Key: Any] = [.font: scaledFont]
        let attributedText = NSMutableAttributedString(string: "Your text here!", attributes: attributes)
        attributedText.append(NSAttributedString(attachment: attachment))
        
        label.adjustsFontForContentSizeCategory = true
        label.attributedText = attributedText
    }
 

至于获得正确字体大小以响应首选内容大小变化的最佳方法,我更喜欢覆盖traitCollectionDidChange类似的内容:

    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        guard previousTraitCollection?.preferredContentSizeCategory
                != traitCollection.preferredContentSizeCategory
        else { return }
        collectionView.collectionViewLayout.invalidateLayout()
    }

但是使用通知应该同样有效:

NotificationCenter.default.addObserver(self,
                   selector: #selector(sizeCategoryDidChange),
                   name: UIContentSizeCategory.didChangeNotification,
                   object: nil)

@objc private func sizeCategoryDidChange() {
            collectionView.collectionViewLayout.invalidateLayout()
        }

推荐阅读