如何将自定义字体和图像添加到我的选择器中?

发布于 2025-01-31 08:10:42 字数 1301 浏览 4 评论 0 原文

我的应用程序中有一个选择器,显示当前选择选项作为其标签。但是,我似乎无法弄清楚如何更改选择器为其标签所使用的字体以及显示所有选择器选项。我还想将一个图标放在选择器按钮的一部分的文本的前面,但不会像文本那样更改,以便用户可以单击文本和图标,以供选择器出现。

这是我为选择器使用的代码:

var fylker = ["Norge", "Akershus", "Agder", "Buskerud", "Finnmark", "Innlandet", "Møre og Romsdal", "Nordland", "Oslo", "Østfold", "Rogaland", "Troms", "Trøndelag", "Vestfold og Telemark", "Vestland"]
Picker("Fylke", selection: $selectedFylke) {
                            ForEach(fylker, id: \.self) {
                                Text($0)
                            }
                        }

我已经尝试了类似的事情:

Picker("Fylke", selection: $selectedFylke) {
                            ForEach(fylker, id: \.self) {
                                Text($0).font(.custom("Custom-Bold", size: 34))
                            }
                        }

没有

Picker("Fylke", selection: $selectedFylke) {
                            ForEach(fylker, id: \.self) {
                                HStack {
                                Image(systemName: "chevron.forward.circle")
                                Text($0)
                                }
                            }
                        }

任何

想法? 谢谢!

I have a picker in my app that displays the currently picked option as its label. However, I can't seem to figure out how to change the font used by the picker for its label and for displaying all the picker options. I would also like to put an icon in front of the text of the picker that's part of the picker button but doesn't change as the text does so that a user could click both the text and the icon for the picker to appear.

Here's the code I am using for my picker:

var fylker = ["Norge", "Akershus", "Agder", "Buskerud", "Finnmark", "Innlandet", "Møre og Romsdal", "Nordland", "Oslo", "Østfold", "Rogaland", "Troms", "Trøndelag", "Vestfold og Telemark", "Vestland"]
Picker("Fylke", selection: $selectedFylke) {
                            ForEach(fylker, id: \.self) {
                                Text($0)
                            }
                        }

I've already tried things like:

Picker("Fylke", selection: $selectedFylke) {
                            ForEach(fylker, id: \.self) {
                                Text($0).font(.custom("Custom-Bold", size: 34))
                            }
                        }

and

Picker("Fylke", selection: $selectedFylke) {
                            ForEach(fylker, id: \.self) {
                                HStack {
                                Image(systemName: "chevron.forward.circle")
                                Text($0)
                                }
                            }
                        }

to no avail

Any ideas?
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

九公里浅绿 2025-02-07 08:10:42

尝试此方法设置 picker 的字体:

struct ContentView: View {

    let fylker = ["Norge", "Akershus", "Agder", "Buskerud", "Finnmark",
                  "Innlandet", "Møre og Romsdal", "Nordland", "Oslo", "Østfold",
                  "Rogaland", "Troms", "Trøndelag", "Vestfold og Telemark", "Vestland"]
    
    @State var selectedFylke = ""
    
    var body: some View {
        Picker("Fylke", selection: $selectedFylke) {
            ForEach(fylker, id: \.self) { item in
                HStack {
                    Image(systemName: "chevron.forward.circle")
                    Text(item)
                }
                .font(.custom("Custom-Bold", size: 34)) // for both the Text and the Image
                .tag(item)
            }
        }.pickerStyle(.wheel) // <-- here
    }
}

这确实不是与PickerStyle一起工作:自动>自动菜单菜单>和分割

try this approach to set the font used by the Picker:

struct ContentView: View {

    let fylker = ["Norge", "Akershus", "Agder", "Buskerud", "Finnmark",
                  "Innlandet", "Møre og Romsdal", "Nordland", "Oslo", "Østfold",
                  "Rogaland", "Troms", "Trøndelag", "Vestfold og Telemark", "Vestland"]
    
    @State var selectedFylke = ""
    
    var body: some View {
        Picker("Fylke", selection: $selectedFylke) {
            ForEach(fylker, id: \.self) { item in
                HStack {
                    Image(systemName: "chevron.forward.circle")
                    Text(item)
                }
                .font(.custom("Custom-Bold", size: 34)) // for both the Text and the Image
                .tag(item)
            }
        }.pickerStyle(.wheel) // <-- here
    }
}

This does not work with pickerStyle: automatic, menu and segmented

栀子花开つ 2025-02-07 08:10:42

据我了解,我认为您可以尝试使用字体修改器添加您的自定义字体并在HSTACK中添加图像,以便它们彼此旁边,例如

import SwiftUI

struct SwiftUIView: View {
var fylker = ["Norge", "Akershus", "Agder", "Buskerud", "Finnmark", 
 "Innlandet", "Møre og Romsdal", "Nordland", "Oslo", "Østfold", 
 "Rogaland", "Troms", "Trøndelag", "Vestfold og Telemark", "Vestland"]
@State var selectedFylke:String = ""

var body: some View {
    Picker("Fylke", selection: $selectedFylke) {
        ForEach(fylker, id: \.self) { I in
            HStack(){
                Text(I).font(Font.custom("Font-Family-Name", size: 16))
                Image(systemName: "pencil")
            }.tag(I)
        }
    }.frame(width: 200, height: 60, alignment:.center).background(Color.red)
    
}
}

  struct SwiftUIView_Previews: PreviewProvider {
    static var previews: some View {
        SwiftUIView()
    }
    }

“

from what i understand i think you could try to use a font modifier to add your custom font and add image inside HStack so they are beside each other like that

import SwiftUI

struct SwiftUIView: View {
var fylker = ["Norge", "Akershus", "Agder", "Buskerud", "Finnmark", 
 "Innlandet", "Møre og Romsdal", "Nordland", "Oslo", "Østfold", 
 "Rogaland", "Troms", "Trøndelag", "Vestfold og Telemark", "Vestland"]
@State var selectedFylke:String = ""

var body: some View {
    Picker("Fylke", selection: $selectedFylke) {
        ForEach(fylker, id: \.self) { I in
            HStack(){
                Text(I).font(Font.custom("Font-Family-Name", size: 16))
                Image(systemName: "pencil")
            }.tag(I)
        }
    }.frame(width: 200, height: 60, alignment:.center).background(Color.red)
    
}
}

  struct SwiftUIView_Previews: PreviewProvider {
    static var previews: some View {
        SwiftUIView()
    }
    }

enter image description here

enter image description here

寂寞清仓 2025-02-07 08:10:42

另外,如果您是要添加选择器旁边的图像,那么它们似乎是按钮拾取器,您可以做

import SwiftUI

struct SwiftUIView: View {
var fylker = ["Norge", "Akershus", "Agder", "Buskerud", "Finnmark", 
 "Innlandet", "Møre og Romsdal", "Nordland", "Oslo", "Østfold", 
  "Rogaland", "Troms", "Trøndelag", "Vestfold og Telemark", "Vestland"]
   @State var selectedFylke:String = ""

 var body: some View {
    HStack(){
    Image(systemName: "pencil").foregroundColor(.white)
    Picker("search", selection: $selectedFylke) {
        ForEach(fylker, id: \.self) {
                Text($0).font(Font.custom("Font-Family-Name", size: 16))
        }
    }
    }.frame(width: 200, height: 60, alignment: 
     .center).background(Color.red).background(Color.red).cornerRadius(30)
   }
 }

struct SwiftUIView_Previews: PreviewProvider {
static var previews: some View {
    SwiftUIView()
   }
 }

“在此处输入图像说明”

“

also if you mean to add the image beside the picker so they seemed as a button picker you can do that

import SwiftUI

struct SwiftUIView: View {
var fylker = ["Norge", "Akershus", "Agder", "Buskerud", "Finnmark", 
 "Innlandet", "Møre og Romsdal", "Nordland", "Oslo", "Østfold", 
  "Rogaland", "Troms", "Trøndelag", "Vestfold og Telemark", "Vestland"]
   @State var selectedFylke:String = ""

 var body: some View {
    HStack(){
    Image(systemName: "pencil").foregroundColor(.white)
    Picker("search", selection: $selectedFylke) {
        ForEach(fylker, id: \.self) {
                Text($0).font(Font.custom("Font-Family-Name", size: 16))
        }
    }
    }.frame(width: 200, height: 60, alignment: 
     .center).background(Color.red).background(Color.red).cornerRadius(30)
   }
 }

struct SwiftUIView_Previews: PreviewProvider {
static var previews: some View {
    SwiftUIView()
   }
 }

enter image description here

enter image description here

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文