【问题标题】:iOS SwiftUI change default Font for every ViewiOS SwiftUI 更改每个视图的默认字体
【发布时间】:2020-03-17 03:39:45
【问题描述】:

我正在尝试为我的应用中的每个视图更改 SwiftUI 中的默认字体

我要避免的是每次都这样设置:

.font(.custom("FONT_NAME", size: 20))

我想要为所有文本视图只更改一次,最重要的是,我想继续使用修饰符:

.font(.caption)

无需将文本视图重置为系统字体。

但我还是没有找到任何解决方案,有人有吗?

【问题讨论】:

    标签: swift text fonts swiftui ios13


    【解决方案1】:

    您可以构建自己的视图修饰符。您可以为private var fontDescriptions: 中的每个 UIFont.TextStyle 定义要使用的大小和字体。

    现在你可以像这样在你的视图上调用这个修饰符。 Text("my text").customFont(.headline)

    请记住,这个也实现了字体缩放功能。

    如果你想成为铁杆,你也可以将你的“customFont”函数称为“字体”。

    extension View {
        func customFont(_ textStyle: UIFont.TextStyle) -> ModifiedContent<Self, CustomFont> {
            return modifier(CustomFont(textStyle: textStyle))
        }
    }
    
    struct CustomFont: ViewModifier {
        let textStyle: UIFont.TextStyle
    
        /// Will trigger the refresh of the view when the ContentSizeCategory changes.
        @Environment(\.sizeCategory) var sizeCategory: ContentSizeCategory
    
        func body(content: Content) -> some View {
    
            guard let fontDescription = fontDescriptions[textStyle] else {
    
                print("textStyle nicht vorhanden: \(textStyle)")
    
                return content.font(.system(.body));
            }
    
            let fontMetrics = UIFontMetrics(forTextStyle: textStyle)
            let fontSize = fontMetrics.scaledValue(for: fontDescription.1)
    
            return content.font(.custom(fontDescription.0, size: fontSize))
        }
    }
    
    /// Define the custom fonts to use, depending on the TextStyle.
    typealias CustomFontDescription = (String, CGFloat)
    private var fontDescriptions: [UIFont.TextStyle: CustomFontDescription] = [
        .headline: ("MYFONT", SIZE),
        .subheadline: ("MYFONT", SIZE),
        ...
    ]
    

    【讨论】:

    • 我整天都在寻找一种解决方案,它可以在您更改字体时动态缩放字体,而不仅仅是在加载时设置它们。这是唯一对我有用的解决方案。干得好!
    【解决方案2】:

    您可以使用视图修饰符来解决您的问题。

    @available(iOS 13.0, *)
    struct CustomText: ViewModifier {
        func body(content: Content) -> some View {
            content
                .font(.custom("FONT_NAME", size: textSize(textStyle: .headline)))
                .background(Color.blue)
                .foregroundColor(Color.white)
               //Other customization
        }
    }
    //For Accessibility feature
    func textSize(textStyle: UIFont.TextStyle) -> CGFloat {
       return UIFont.preferredFont(forTextStyle: textStyle).pointSize
    }
    @available(iOS 13.0, *)
    struct ContentView: View {
        var body: some View {
            Text("Hello, SwiftUI")
                .modifier(CustomText())
                .font(.caption) // You can still change the font .
        }
    }
    
    @available(iOS 13.0, *)
    struct SwiftUIView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }
    

    【讨论】:

    • 这个缺少文本的辅助功能。我认为合并我们的两个答案是正确的答案。我的用于覆盖 .font(.caption) 和你的用于通过 body 修饰符设置默认字体。
    • 我已更新我的答案以支持无障碍功能@Mamaessen
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-07
    • 2017-01-28
    • 2012-03-29
    • 2014-09-27
    • 1970-01-01
    相关资源
    最近更新 更多