SwiftUI Text元件使用google fonts的字體
xcode 13.4.1, swift 5.5, iOS 15.4
2022-08-16
顯示Text的元件,應該是所有App最常用的元件之一,這次講講Text元件怎樣使用第三方的字體樣式。
1. 先到google fonts去下載你想要的字體庫。
這次我使用
HomemadeApple這個字體庫,下載完後,把相關的字體庫檔案放到xcode的project中。

2. 先放一個Text和你想要的文字。
import SwiftUI
struct CustomFontExample: View {
var body: some View {
Text("Stay hungry stay foolish")
}
}
3. 為了能使程式看到新加字體庫的檔案我們需要在Targets->Info->Custom iOS Target Properties->Fonts provided by application增加一個item:

4. 然後在Targets->Build Phases->Copy Bundle Resources按+,把該檔案添加進來。

5. 最後就可以使用Font.custom來引用這個新字體庫:
import SwiftUI
struct CustomFontExample: View {
var body: some View {
Text("Stay hungry stay foolish")
.font(.custom("HomemadeApple-Regular", size: 40))
}
}