3

enter image description here

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?

XLE_22
  • 5,124
  • 3
  • 21
  • 72
WishIHadThreeGuns
  • 1,225
  • 3
  • 17
  • 37

2 Answers2

8

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.

XLE_22
  • 5,124
  • 3
  • 21
  • 72
0

You can use a custom font for this.

Step 1: Add Custom fonts to the project.

enter image description here

Step 2: Add these fonts under "Fonts Provided by application" key in info.plist file

enter image description here

Step 3: Use it as below.

For Bold font:

artistLabel.font = UIFont(name:"ArchSans-Bold", size: fontSize)

For Regular Font:

artistLabel.font = UIFont(name:"ArchSans", size: fontSize)
Ritu pal
  • 306
  • 2
  • 11