Display around fonts in Japanese with Swift: macOS

Execution environment

Overview

--Get the font family and make it ** Japanese ** --Display a list of available font styles --Output font name in Japanese

code

Get the available font families

--fontFamilies List of available font families --localizedFamilyName Font localization (Japanese this time)

let fontFamilies = NSFontManager.shared.availableFontFamilies
for family in fontFamilies {
    print(family)//Font family name
    let localizedFamilyName = NSFontManager.shared.localizedName(forFamily: family, face: nil)
    print(localizedFamilyName)//Font family name(After localization)
}

Explanation

NSFontManager.shared.availableFontFamilies --Get the available font family.

localizedName(forFamily:face:) --Localize font family names to your language

localizedName(forFamily:face:) Returns a localized string with the name of the specified font family and face, if one exists.

Return Value A localized string with the name of the specified font family and face, or, if face is nil, the font family only.

From AppleDocumentation / NSFontmanager

If you pass nil to the argument face:, you can only get the localized font family.

Show available font styles

--availableMembers (ofFontFamily :) Get available members from the font family --Get the localized name from the displayName instance of NSFont

Sample showing more available members from the font family
let fontFamilies = NSFontManager.shared.availableFontFamilies
for family in fontFamilies {
    let localizedFamilyName = NSFontManager.shared.localizedName(forFamily: family, face: nil)
    print(localizedFamilyName)//Font family name
    
    //Get available members from font family name
    let FontMembers = NSFontManager.shared.availableMembers(ofFontFamily: family)
    for i in 0 ..< FontMembers!.count{
        let Font = FontMembers![i][0] as! String//Get the members available here
        let FontName = NSFont(name: Font, size: NSFont.systemFontSize)//Create an instance of NSFont
        print(FontName?.displayName)
    }
}

Explanation

availableMembers(ofFontFamily:) --Specify a font family to get the members of available fonts

For example, if you call availableMembersOfFontFamily:@"Times", it might return an array like this: (("Times-Roman", "Roman", 5, 4), ("Times-Italic", "Italic", 6, 5), ("Times-Bold", "Bold", 9, 2), ("Times-BoldItalic", "Bold Italic", 9, 3) )

From AppleDocumentation / available members

.displayName --Get the localized font name from an instance of NSFont

Remarks

Not all font files are complete, so you may really need to handle the error.

Please point out any mistakes.

reference

https://developer.apple.com/documentation/appkit/nsfontmanager https://developer.apple.com/documentation/appkit/nsfont

https://stackoverflow.com/questions/2977915/how-can-i-get-a-list-of-the-available-nsfont-families

Recommended Posts

Display around fonts in Japanese with Swift: macOS
Customize View with View Modifier in Swift UI
Manage View status with enum in Swift
How to display error messages in Japanese
Display images with exact AutoLayout ratio using image loading library Nuke in Swift
Settings to display Japanese Javadoc in IntelliJ IDEA
[Japanese localization] i18n rails Easy Japanese localization view display only
[Swift] Display Hello Swift!
How to implement UICollectionView in Swift with code only
Display the list in setDetails on the screen with spring-security
Display 3D objects of SceneKit with Swift UI, etc.