ios - 更改首选内容大小后,我的一些可重用单元格标签中的字体大小仍然不变
问题描述
我正在使用具有包含 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 以调整大小。
解决方案
当首选内容大小更改时,使属性字符串调整大小。
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()
}
推荐阅读
- python - 它计算数据框每一列中缺失值的总数
- python - 使用 Python3 删除新行
- python-3.x - 为什么导入 matplotlib.pyplot 会改变后端?
- java - 如何从无 gui 类实时更新 Java Jframe 控件
- java - JUnit4 测试期间与数据库的连接
- webpack - webpack - 从命令行关闭手表
- excel - 没有数据传输到带有 ADODB 记录集的 Access
- asp.net - 在asp中用逗号添加数字
- python - 我正在尝试通过 python 程序读取文件的内容
- python - pyspark 将数据帧写入 hdfs 失败