在SwiftUI Xcode beta 5中将带有cornerRadius的边框添加到图像

jsb*_*eJS 0 iphone beta swift swiftui xcode11

如何cornerRadius为图像添加边框。我收到不赞成使用的警告,说我应该使用RoundedRectange Shape,但是我不知道该怎么使用

Beta 4:

Image(uiImage: ...)
   .border(Color.black, width: 2, cornerRadius: 10)
Run Code Online (Sandbox Code Playgroud)

Mar*_*ens 59

SwiftUI 1.0

使用cornerRadius 和overlay Modifiers

这是我们可以使用 cornerRadius 修改器(剪辑视图)然后用颜色覆盖笔划的另一种方法。

VStack(spacing: 40) {
    Text("Image Border").font(.largeTitle)
    Text("Using cornerRadius & overlay").font(.title).foregroundColor(.gray)
    Text("Using cornerRadius will also clip the image. Then overlay a border.")
        .frame(minWidth: 0, maxWidth: .infinity)
        .font(.title)
        .padding()
        .background(Color.orange)
        .foregroundColor(.black)

    Image("profile")
        .cornerRadius(10)
        .overlay(RoundedRectangle(cornerRadius: 10)
            .stroke(Color.orange, lineWidth: 4))
        .shadow(radius: 10)
}
Run Code Online (Sandbox Code Playgroud)

结果

在此处输入图片说明

  • 它优先于同一父视图中的其他视图分配可用空间。(默认情况下,其余子视图的布局优先级为零。)如果没有这个,该文本视图将仅显示一行文本并被截断。 (2认同)

Kev*_*n R 18

考虑一下:向视图添加修饰符将返回一个View包含前一个实例的新实例。这也是添加修饰符的顺序很重要的原因。

我们可以利用这个优势:通过添加填充,然后为我们的新视图添加背景,我们可以创建我们自己的附加层:

Image("cat")
    .cornerRadius(7) // Inner corner radius
    .padding(5) // Width of the border
    .background(Color.primary) // Color of the border
    .cornerRadius(10) // Outer corner radius
Run Code Online (Sandbox Code Playgroud)

结果是:

在此处输入图片说明

您甚至可以在 ViewModifier 中将其转换为更容易重用:

struct RoundedEdge: ViewModifier {
    let width: CGFloat
    let color: Color
    let cornerRadius: CGFloat

    func body(content: Content) -> some View {
        content.cornerRadius(cornerRadius - width)
            .padding(width)
            .background(color)
            .cornerRadius(cornerRadius)
    }
}
Run Code Online (Sandbox Code Playgroud)

使用它会变成:

Image("cat").modifier(RoundedEdge(width: 5, color: .black, cornerRadius: 20))
Run Code Online (Sandbox Code Playgroud)

这适用于任何 SwiftUI 视图,例如Text

Text("Some text")
    .padding(15)
    .background(Color.red)
    .modifier(RoundedEdge(width: 5, color: .black, cornerRadius: 20))
Run Code Online (Sandbox Code Playgroud)

结果是:

在此处输入图片说明


kon*_*iki 9

首先,请注意,您执行此操作的方式不是剪切图像。也许您没有注意到图像是否太小,或者其背景与画布的颜色相同。但是即使使用beta 4语法,也需要添加.clipShape()


回到您的问题,根据Beta 5发行说明:

不建议使用background(:alignment :)和border(:width :)修饰符的复杂重载。在背景(:alignment :)或overlay(:alignment :)中使用形状来绘制形状。(53067530)

因此,模式将如下所示:

.overlay(RoundedRectangle(...).stroke(...).foregroundColor(...))
Run Code Online (Sandbox Code Playgroud)

在您的特定情况下:

Image("mypic").resizable().frame(width: 300, height: 300)
    .clipShape(RoundedRectangle(cornerRadius: 30))
    .overlay(RoundedRectangle(cornerRadius: 30).stroke(lineWidth: 2).foregroundColor(Color.black))
Run Code Online (Sandbox Code Playgroud)


小智 5

编写一个与 Image View 相同的圆形文本视图

struct RoundedTextView: View {
    var body: some View {
        Text("Rounded Text View")
            .frame(width: 200, height: 200, alignment: .center)
            .foregroundColor(.white)
            .background(Color.blue)
            .cornerRadius(16)
            .overlay(
                RoundedRectangle(cornerRadius: 16).stroke(Color.yellow, lineWidth: 8)
            )
    }
}
Run Code Online (Sandbox Code Playgroud)

像这样的图像预览:

在此处输入图片说明