Swift:`Text()`未调整字体

问题描述 投票:0回答:1

这段代码的预期结果是让每种字体都显示自己的字体

Picker
:

enter image description here

但是,由于某种原因,下面的代码不会产生此结果,而只是以默认字体显示它们:

// Fonts for widget and widget preview
let availableFonts = [
    "Georgia", "Times New Roman",
    "Verdana", "Palatino", "Baskerville", "Didot",
    "Optima", "Arial"
]
Picker("", selection: $selectedFontIndex) {
    ForEach(0..<availableFonts.count, id: \.self) { index in
        Text(availableFonts[index])
            .font(Font.custom(availableFonts[index], size: 16))
    }
}

我想指出,下面的代码确实正确地切换了该文本的字体:

Text("More is lost by indecision than by wrong decision.")
                                .font(Font.custom(availableFonts[selectedFontIndex], size: 16))

enter image description here

swift swiftui
1个回答
0
投票

这只是我快速整理的东西,因此可以对其进行微调和进一步开发,但也许它可以帮助您入门:


import SwiftUI

struct PickerFontsView: View {
    
    // Fonts for widget and widget preview
    let availableFonts = [
        "Georgia", "Times New Roman",
        "Verdana", "Palatino", "Baskerville", "Didot",
        "Optima", "Arial"
    ]
    
    @State private var selectedFontIndex = 0
    @State private var showMenu = false
    var body: some View {
        
        VStack {
            Button {
                withAnimation {
                    showMenu.toggle()
                }
            } label: {
                HStack {
                    Text(availableFonts[selectedFontIndex])
                        .font(Font.custom(availableFonts[selectedFontIndex], size: 20))
                    Image(systemName: "chevron.up.chevron.down")
                        .font(.system(size: 13))
                }
                .tint(.white)
                
            }
            .overlay(alignment: .bottom) {
                if showMenu {
                    VStack(spacing: 5) {
                        ForEach(0..<availableFonts.count, id: \.self) { index in
                            Button {
                                withAnimation {
                                    selectedFontIndex = index
                                    showMenu.toggle()
                                }
                            } label : {
                                VStack(spacing: 5) {
                                    HStack {
                                        Text(availableFonts[index])
                                            .font(Font.custom(availableFonts[index], size: 20))
                                        if selectedFontIndex == index {
                                            Image(systemName: "checkmark")
                                        }
                                    }
                                    Divider()
                                }
                            }
                            .tint(.black)
                        }
                    }
                    .fixedSize()
                    .padding()
                    .background(.thinMaterial, in: RoundedRectangle(cornerRadius: 20))
                    .offset(y: -30)
                }
            }
            
            Text("More is lost by indecision than by wrong decision.")
                .font(Font.custom(availableFonts[selectedFontIndex], size: 16))
                .frame(width: 300)
                .padding()
                .background(.thinMaterial, in: RoundedRectangle(cornerRadius: 20))
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
        .background(Color.yellow)
        .ignoresSafeArea()
        
        
        
        
    }
}

#Preview {
    PickerFontsView()
}

它不使用选择器,但是,您可以将其转换为带有一些参数的结构,并且基本上是一个自定义选择器。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.