SwiftUI如何在macOS上改变文本大小

4

我在编写一个小的 SwiftUI 应用程序时发现,在 macOS 上字体大小非常小

var body: some View {
        VStack {
            NavigationView {
                List(fetch.Novitadss) { Novitads in
                    VStack(alignment: .leading) {
                        // 3.
                        Text(Novitads.title)
                            .font(.headline)
                            .fontWeight(.regular)
                            .onTapGesture {
                                UIApplication.shared.open(URL(string: Novitads.link)!)
                            }
                    }
                }

我想添加一个条件来改变 .font(.headline) 的样式,如果操作系统是 macOS,因此我尝试了以下代码:

                         #if os(iOS)
                            .font(.headline)
                        #elseif os(macOS)
                               .font(.title)
                        #endif

但是我收到了“意外的平台条件(期望是 'os'、'arch' 或 'swift')”的错误消息。
3个回答

3

您在评论中提到了为何代码无法正常工作的问题,我会在我的回答中进行解释。

根据文档,条件编译块具有以下语法:

enter image description here

请注意,块内部写着“语句”。.font(.headline)不是语句,因此将其放在那里是非法的。这也可以从页面下方的正式语法符号中看出:

enter image description here

此外,在正式语法之前的“注释”块中:
每个条件编译块中的语句都会被解析,即使它们没有被编译。但是,如果编译条件包括swift()平台条件,则有一种例外情况。
这就解释了为什么您的#elseif块也出现了错误。
换句话说,#if语句不是C++预处理器指令 :)
因此,要修复问题,您必须将一个完整的语句放在这些块中。一种(愚蠢的)解决方法是:
 #if os(iOS)
Text(Novitads.title)
    .font(.headline)
    .fontWeight(.regular)
    .onTapGesture {
        UIApplication.shared.open(URL(string: Novitads.link)!)
    }
#elseif os(macOS)
Text(Novitads.title)
    .font(.title)
    .fontWeight(.regular)
    .onTapGesture {
        // UIApplication.shared.open(URL(string: Novitads.link)!)
        // you would use this on macOS to open URLs, right?
        NSWorkspace.shared.open(URL(string: Novitads.link)!)
    }
#endif

当然,Asperi提取platformFont方法的解决方案要好得多。

1
你可以使用以下助手扩展。
extension Text {
    func platformFont() -> Text {
#if canImport(AppKit) || targetEnvironment(macCatalyst)
        return self.font(.title)
#elseif canImport(UIKit)
         return self.font(.headline)
#else
        return self
#endif
    }
}

和使用

Text(Novitads.title)
   .platformFont()

更新:添加了演示。已测试通过 Xcode 11.4 / macOS 10.15.4。

demo

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Hello World!")
            Text("Hello World!").platformFont()
        }
    }
}

在编程中,优先选择 canImport 而不是 os,是否有原因? - Sweeper
@Sweeper canImportos更好,因为你的测试基于功能而不是操作系统。如果UIKit在macOS上可用,您无需更改代码即可使用它。 - Nicolas Mandica
@Asperi 我仔细使用了相同的代码,唯一不同的是我的标题在列表中。可能对于列表来说它不起作用吗? - devnull
@devnull,嗯...只需将这两个演示文本放入List中,而不是VStack,并将List放入NavigationView中,所有内容都可以正常应用每种字体。...我突然想到,你是在Mac/Catalyst上运行它吗?因为我正在开发原生的macOS应用程序。 - Asperi
@Asperi,不好意思再次打扰您,但我很想知道出了什么问题 :) 请查看代码和截图 https://gist.github.com/gurugeek/28e5e1a775e01554bd783dd678a068d6 我做错了什么吗?在iOS上调整大小正常... Xcode 11.4.1 运行在 Catalina 10.15.4 上。 - devnull
显示剩余7条评论

1
请查看this article。应该是这样的:
#if targetEnvironment(macCatalyst)
return self.font(.largeTitle)
#elseif os(iOS)
return self.font(.system(size: 10))
#else
return self
#endif

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接