简体   繁体   中英

Custom font size for Text in SwiftUI

I have a label in my view that I want to use the system font size in medium, with a size of 21 points.
I created a custom extension to re-use the font created:

extension Font {
    static var primaryButton: Font {
        return Font.custom("SFUIDisplay-Light", size: 21)
    }
}

However, this does not have any effect. I changed the string to HelveticaNeue-UltraLight and it did work, so I'm guessing that SFUIDisplay-Light is simply the incorrect font name.
In font book, it says SFProText-Light , but that also did not work for me.

What is the correct font name of the San Francisco font in SwiftUI?
Or: how can I set the font size using the system font?

您可以使用以下命令为系统字体设置显式大小:

.font(.system(size: 60))

您可以像这样设置自定义字体大小,

.font(.custom("FONT_NAME", size: 20))
Text("Default font design").font(Font.system(size:30, design: .default))
Text("Monospaced font design").font(Font.system(size:30, design: .monospaced))
Text("Rounded font design").font(Font.system(size:30, design: .rounded))
Text("Serif font design").font(Font.system(size:30, design: .serif))

Text("Large Title").font(.largeTitle)
Text("Title").font(.title)
Text("Headline").font(.headline)
Text("SubHeadline").font(.subheadline)
Text("Body").font(.body)
Text("Callout").font(.callout)
Text("Caption").font(.caption)
Text("FootNote").font(.footnote)

Text("Custom font").font(Font.custom("OpenSans-Bold", size: 12.3))

more detail Please follow: https://github.com/arjun011/SwiftUI-Controls

// Using system font "San Francisco"
let fontSystem = Font.system(size: 13.0)

// Using installed custom font
let fontCustom = Font.custom("YOUR FONT NAME", size: 13.0)

You can place the font* constant to where you need to set the font style.

Read more from "Getting System Fonts" section in https://developer.apple.com/documentation/swiftui/font

If you want to set a custom font for your text let's say Helvetica Neue then in that case you can do something like this

Text("Hello World")
.font(.custom("Helvetica Neue", size: 20))

If you just want to go with system font then in that case you can do something like this

Text("Hello World")
.font(.system(size: 20, weight: .light, design: .default))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM