[英]SwiftUI change text size for macOS
Using SwiftUI for a small all and I noticed that the font size is very small on macOS使用 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)!)
}
}
}
I would like to add a condition to change the.font(.headline) if os is macOS so I tried如果 os 是 macOS,我想添加一个条件来更改 .font(.headline) 所以我尝试了
#if os(iOS)
.font(.headline)
#elseif os(macOS)
.font(.title)
#endif
but I get "Unexpected platform condition (expected 'os', 'arch', or 'swift')"但我得到“意外的平台条件(预期的'os'、'arch'或'swift')”
From your comment , you asked why your code did not work, which I will explain in my answer.从您的评论中,您询问了为什么您的代码不起作用,我将在回答中对此进行解释。
According to the documentation , a conditional compilation block has the following syntax:根据文档,条件编译块具有以下语法:
Notice that it says "statements" inside the block.请注意,它在块内显示“语句”。 .font(.headline)
is not a statement, so it is illegal to put that there. .font(.headline)
不是声明,因此将其放在那里是非法的。 This is also evident in the formal grammar notation further down the page:这在页面下方的正式语法符号中也很明显:
In addition, in the "Note" block just above the formal grammar:此外,在形式语法正上方的“注释”块中:
Each statement in the body of a conditional compilation block is parsed even if it's not compiled.条件编译块主体中的每个语句都会被解析,即使它没有被编译。 However, there is an exception if the compilation condition includes a
swift()
platform condition.但是,如果编译条件包含swift()
平台条件,则会出现异常。
This explains why your #elseif
block has the error as well.这解释了为什么您的#elseif
块也有错误。
In other words, #if
statements are not your C++ preprocessor directives:)换句话说, #if
语句不是您的 C++ 预处理器指令:)
Therefore, to fix the problem, you must put a whole statement in those blocks.因此,要解决此问题,您必须在这些块中放入完整的语句。 One (dumb) way to fix this would be:解决此问题的一种(愚蠢)方法是:
#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
Of course, Asperi's solution of extracting a platformFont
method is a lot better.当然,Asperi 的解压platformFont
方法的方案要好很多。
You can use the following helper extension您可以使用以下辅助扩展
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
}
}
and usage和使用
Text(Novitads.title)
.platformFont()
Update: Added demo.更新:添加了演示。 Tested with Xcode 11.4 / macOS 10.15.4使用 Xcode 11.4 / macOS 10.15.4 测试
struct ContentView: View {
var body: some View {
VStack {
Text("Hello World!")
Text("Hello World!").platformFont()
}
}
}
Please check this article It should be like this:请查看这篇文章它应该是这样的:
#if targetEnvironment(macCatalyst)
return self.font(.largeTitle)
#elseif os(iOS)
return self.font(.system(size: 10))
#else
return self
#endif
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.