Macos Swiftui自定义UI Textfield字段带有圆形边缘

发布于 2025-01-17 20:54:58 字数 1913 浏览 5 评论 0原文

我在自定义 TextField 字段时遇到问题,我想尝试为两者设置与第一张图像相同的边框:

  • 边框颜色
  • 边缘厚度尺寸
  • 边缘圆角类型

有人能给我建议吗?

我想达到的结果:

在此处输入图像描述

我得到的结果:

在此处输入图像描述

                    HStack(alignment: .center) {
                        Spacer()
                        TextField("Git Repository URL", text: $repoUrlStr)
                            .textFieldStyle(PlainTextFieldStyle())
                            .lineLimit(1)
                            .frame(width: 300)
                            .onAppear {
                                let url = "https://github.com/name/name"
                                if isValid(url: url) == true {
                                    print("ok", url)
                                }
                            }
                            Button {
                                if !repoUrlStr.isEmpty {
                                    self.repoUrlStr = ""
                                }
                            } label: {
                                Image(systemName: "xmark.circle.fill")
                                    .padding(.trailing, 2)
                                    .font(.system(size: 12))
                            }
                            .buttonStyle(PlainButtonStyle())
                            .opacity(repoUrlStr.isEmpty ? 0 : 1)
                        Spacer()
                    }
                    .padding([.top, .bottom], 4)
                    .border(Color.gray, width: 2)
                    .cornerRadius(3)
                    .padding(.bottom, 15)

I'm having trouble customizing a TextField field, I would like to try to have a border equal to that of the first image for both:

  • Border color
  • Edge thickness dimension
  • Type of edge rounding

Can anyone give me advice?

Result I would like to achieve:

enter image description here

Result I get:

enter image description here

                    HStack(alignment: .center) {
                        Spacer()
                        TextField("Git Repository URL", text: $repoUrlStr)
                            .textFieldStyle(PlainTextFieldStyle())
                            .lineLimit(1)
                            .frame(width: 300)
                            .onAppear {
                                let url = "https://github.com/name/name"
                                if isValid(url: url) == true {
                                    print("ok", url)
                                }
                            }
                            Button {
                                if !repoUrlStr.isEmpty {
                                    self.repoUrlStr = ""
                                }
                            } label: {
                                Image(systemName: "xmark.circle.fill")
                                    .padding(.trailing, 2)
                                    .font(.system(size: 12))
                            }
                            .buttonStyle(PlainButtonStyle())
                            .opacity(repoUrlStr.isEmpty ? 0 : 1)
                        Spacer()
                    }
                    .padding([.top, .bottom], 4)
                    .border(Color.gray, width: 2)
                    .cornerRadius(3)
                    .padding(.bottom, 15)

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

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

发布评论

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

评论(1

青朷 2025-01-24 20:54:58

尝试在背景中使用圆角矩形(带有所需的参数,如角半径、颜色等)而不是边框​​,例如

.background(
    RoundedRectangle(cornerRadius: 8)
        .stroke(your_color_here)
)

Try to use rounded rectangle in background (with needed parameters, like corner radius, color, etc) instead of border, like

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