作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何在 UILabel
上使用动态字体,我们有以下内容:
artistLabel.font = UIFont.preferredFont(forTextStyle: .body, compatibleWith: UITraitCollection(legibilityWeight: .regular))
artistLabel.adjustsFontForContentSizeCategory = true
trackLabel.font = UIFont.preferredFont(forTextStyle: .body, compatibleWith: UITraitCollection(legibilityWeight: .bold))
trackLabel.adjustsFontForContentSizeCategory = true
粗体和常规看起来一样吗?如何获得真正的“粗体”字体?
最佳答案
bold and regular look the same?
你做出这种说法是因为我认为你误用了元素来传入 preferredFont
方法的传入参数 compatibleWith
。
您已根据需要提供了 UITraitCollection
元素,这就是您的代码编译的原因。
太好了 👍 但是这个特征必须是一个内容大小类别才能被系统很好地理解⟹ Apple doc . 🤓
例如,您应该以这种方式使用指定的方法:
artistLabel.font = UIFont.preferredFont(forTextStyle: .body,
compatibleWith: UITraitCollection(preferredContentSizeCategory: .large))
How can I get a true "bold" font?
您可以使用@Ritupal 提供的高效代码👏 或者您可以尝试从 WWDC 2020 视频中提取的以下代码(The details of UI typography) 以获得更强调的文本样式:
if let artistDescriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: .body).withSymbolicTraits(.traitBold) {
artistLabel.font = UIFont(descriptor: artistDescriptor,
size: 0.0)
}
即使已经提供了解决方案,我认为补充这个答案以解释为什么您的初始代码不起作用同时引入另一种方式来强调具有粗体特征的文本很重要。 😉
关于swift - Swift 中的粗体动态类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66472605/
下面是我用来制作 1px 文本描边轮廓的代码。但是如何使轮廓变粗呢?如果我只是用“5px”替换所有“1px”,结果看起来很疯狂。 HTML Hello! CSS .element { color:
我是一名优秀的程序员,十分优秀!