簡體   English   中英

自定義 UIFont SwiftUI & Xcode 14.1

[英]Custom UIFont SwiftUI & Xcode 14.1

首先,我知道這樣的問題以前曾被問過,但在查看了所有答案后我仍然遇到問題。

我正在使用 Xcode 14.1 在 SwiftUI 中編寫我的第一個項目,並希望添加自定義字體。 我首先在項目中創建了一個Fonts組,並使用Add Files to "Project"將 .ttf 文件添加到項目中,確保選中了“ Copy items if needed ”。

然后我將Fonts provided by application添加到Custom iOS Target Properties和字體的數組項,在本例中為Poppins ,因此值讀取為Fonts/Poppins-Regular.ttf

    <key>UIAppFonts</key>
    <array>
        <string>Fonts/Poppins-Black.ttf</string>
        <string>Fonts/Poppins-BlackItalic.ttf</string>
        <string>Fonts/Poppins-Bold.ttf</string>
        <string>Fonts/Poppins-BoldItalic.ttf</string>
        <string>Fonts/Poppins-ExtraBold.ttf</string>
        <string>Fonts/Poppins-ExtraBoldItalic.ttf</string>
        <string>Fonts/Poppins-ExtraLight.ttf</string>
        <string>Fonts/Poppins-ExtraLightItalic.ttf</string>
        <string>Fonts/Poppins-Italic.ttf</string>
        <string>Fonts/Poppins-Light.ttf</string>
        <string>Fonts/Poppins-LightItalic.ttf</string>
        <string>Fonts/Poppins-Medium.ttf</string>
        <string>Fonts/Poppins-MediumItalic.ttf</string>
        <string>Fonts/Poppins-Regular.ttf</string>
        <string>Fonts/Poppins-SemiBold.ttf</string>
        <string>Fonts/Poppins-SemiBoldItalic.ttf</string>
        <string>Fonts/Poppins-Thin.ttf</string>
        <string>Fonts/Poppins-ThinItalic.ttf</string>
    </array>

但是,當在我的App swift 文件中調用此 function 的init()方法中打印出我的 fonts 時:

    func printFonts() {
        let fontFamilyNames = UIFont.familyNames
        
        for familyName in fontFamilyNames {
            print("----------")
            print("Font Family Name -> [\(familyName)]")
            let names = UIFont.fontNames(forFamilyName: familyName)
            print("Font Names ==> [\(names)]")
        }
    }

它不是 output 我的自定義字體,同樣在我的視圖中使用該字體時它不會呈現:

Text("Hello!")
    .font(Font.custom("Poppins-Regular", size: 18))

有趣的是,Xcode 中的預覽不起作用,因為將此屬性添加到報告以下錯誤的視圖中的文本: Compiling failed: ambiguous use of 'font'

我已經搜索和搜索並嘗試了許多兔子洞解決方案,有人可以提供建議嗎?

您不需要在字體數組中包含組名,例如

<string>Poppins-Black.ttf</string>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM