iOS Development

ios – The right way to show non-slashed zero character in Swift for the system font SF Mono

Spread the love


This reply makes use of the Stylistic Alternate options characteristic in CoreText.


Supply:

This hyperlink describes the set of at the moment outlined typographic and format options obtainable from functions utilizing CoreText.

https://developer.apple.com/fonts/TrueType-Reference-Handbook/RM09/AppendixF.html#Type35


Do that code:

import UIKit

class ViewController: UIViewController {
    
    // Set the standing bar of the view to hidden
    override func viewWillAppear(_ animated: Bool) {
        tremendous.viewWillAppear(animated)
        self.setNeedsStatusBarAppearanceUpdate()
    }
    override var prefersStatusBarHidden : Bool {
        return true
    }
    override var childForStatusBarHidden : UIViewController? {
        return nil
    }

    override func viewDidLoad() {
        tremendous.viewDidLoad()
        
        // Set the background shade of the view to systemGray5
        view.backgroundColor = .systemGray5

        // Create a UIButton
        let button = UIButton(sort: .system)

        // Set the title of the button to "100"
        button.setTitle("100", for: .regular)

        // Set the font of the button title to SFMono with a dimension of 150
        button.titleLabel?.font = UIFont.monospacedSystemFont(ofSize: 150, weight: .common)

        // Set the background shade of the button to systemYellow
        button.backgroundColor = .systemYellow

        // Set the tint shade of the button to black
        button.tintColor = .black

        // Make the button rounded by setting its nook radius to 25
        button.layer.cornerRadius = 25

        // Make sure that the rounded corners are displayed appropriately
        button.clipsToBounds = true

        // Outline the font descriptor for the customized font
        let fontDescriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: .physique).withDesign(.monospaced)?.addingAttributes([
            .featureSettings: [
                
                // Set the stylistic alternative for "0"
                [
                UIFontDescriptor.FeatureKey.type: kStylisticAlternativesType,
                UIFontDescriptor.FeatureKey.selector: kStylisticAltThreeOnSelector
                ],
                
                // Set the stylistic various for "1"
                [
                UIFontDescriptor.FeatureKey.type: kStylisticAlternativesType,
                UIFontDescriptor.FeatureKey.selector: kStylisticAltFourOnSelector
            ]
                
            ]
        ])

        // Create the customized font
        let font = UIFont(descriptor: fontDescriptor!, dimension: 150)

        // Create an attributed string with the customized font
        let attributedString = NSAttributedString(string: "100", attributes: [.font: font])

        // Apply the attributed string to the button
        button.setAttributedTitle(attributedString, for: .regular)

        // Add the button to the view
        view.addSubview(button)

        // Middle the button within the view
        button.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            button.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            button.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        ])
    }
}

Operating the code displaying stylistic options for “0” and “1”:

enter image description here

Leave a Reply

Your email address will not be published. Required fields are marked *