How come for a dynamic font on a UILabel
where we have the following:
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? How can I get a true "bold" font?
bold and regular look the same?
You make this statement because I think you misuse the element to pass in the incoming parameter compatibleWith
of the preferredFont
method.
You have provided a UITraitCollection
element as desired and that's why your code compile.
That's great 👍 but this trait must be a content size category to be well understood by the system ⟹ Apple doc. 🤓
You should have used the specified method this way for instance:
artistLabel.font = UIFont.preferredFont(forTextStyle: .body,
compatibleWith: UITraitCollection(preferredContentSizeCategory: .large))
How can I get a true "bold" font?
You can use the code provided by @Ritupal that is efficient 👏 or you can try the following one extracted from a WWDC 2020 video (The details of UI typography) to get a more emphasized text style:
if let artistDescriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: .body).withSymbolicTraits(.traitBold) {
artistLabel.font = UIFont(descriptor: artistDescriptor,
size: 0.0)
}
Even if a solution has already been provided, I deemed it important to complement this answer to explain why your initial code didn't work while bringing another way to emphasize text with bold trait. 😉
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With