Current font used in a Text object in SwiftUI

不羁岁月 提交于 2020-12-15 07:02:14

问题


Is there a way to get/obtain the current font used in a Text object in SwiftUI? ex. Text("abc").font() does not work.


回答1:


The current font is accessible from the environment:

struct ChildView: View {
    @Environment(\.font) var font

    var body: some View {
        Text("Italic version of the hierarchy's font")
            .font((font ?? .body).italic())
    }
}

See https://developer.apple.com/documentation/swiftui/environmentvalues for the full list of available keys, they can come in handy.




回答2:


You can use systemFont.

 Text("ddd").font(Font.system(size: 50))

So you don't need to know exact name of the font.



来源:https://stackoverflow.com/questions/58681801/current-font-used-in-a-text-object-in-swiftui

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