How to use SF Rounded font in SwiftUI?

时光怂恿深爱的人放手 提交于 2020-01-21 19:00:24

问题


I am trying to use the SF rounded font in my SwiftUI project, how would you set it?

I already tried messing around with the .font() but it didn't work (I wasn't able to set it to this rounded font)


回答1:


Text("Your Text").font(.system(.body, design: .rounded))



回答2:


I know the question is about SwiftUI, but I thought it could be helpful to also include an UIKit answer.

let fontSize: CGFloat = 24

// Here we get San Francisco with the desired weight
let systemFont = UIFont.systemFont(ofSize: fontSize, weight: .regular)

// Will be SF Compact or standard SF in case of failure.
let font: UIFont

if let descriptor = systemFont.fontDescriptor.withDesign(.rounded) {
    font = UIFont(descriptor: descriptor, size: fontSize)
} else {
    font = systemFont
}



回答3:


Converted the UIKit answer by @Pomme2Poule into a function for easy use, should anyone need it. Function uses dynamic type too, so it'll scale with the font size.

func roundedFont(ofSize style: UIFont.TextStyle, weight: UIFont.Weight) -> UIFont {
    // Will be SF Compact or standard SF in case of failure.
    let fontSize = UIFont.preferredFont(forTextStyle: style).pointSize
    if let descriptor = UIFont.systemFont(ofSize: fontSize, weight: weight).fontDescriptor.withDesign(.rounded) {
        return UIFont(descriptor: descriptor, size: fontSize)
    } else {
        return UIFont.preferredFont(forTextStyle: style)
    }
}



回答4:


Doing it this way works for me on a Text object in SwiftUI:

.font(.system(size: 13.0, weight: .regular, design: .rounded))



来源:https://stackoverflow.com/questions/56550064/how-to-use-sf-rounded-font-in-swiftui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!