Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI - Add Border to One Edge of an Image

Tags:

It's a pretty straight-forward question - How does one apply a border effect to only the wanted edges of an Image with SwiftUI?

For example, I only want to apply a border to the top and bottom edges of an image because the image is taking up the entire width of the screen.

Image(mission.missionImageString)     .resizable()     .aspectRatio(contentMode: .fit)     .border(Color.white, width: 2) //Adds a border to all 4 edges 

Any help is appreciated!

like image 449
Justin Frazer Avatar asked Oct 30 '19 19:10

Justin Frazer


People also ask

How to give border to image in SwiftUI?

To set border for Image, add border() modifier to the Image. You may specify the border color and border width via border() .

How to add a border in SwiftUI?

Tip: Use stroke() or strokeBorder() with shapes, and border() with other view types. SPONSORED Runway is the control center for your entire mobile team and toolchain. Build and release more confidently with centralized collaboration and end-to-end automation.

How to set border to view SwiftUI?

But the shape view is not a border, nor it draws one automatically; it's just a view having the given shape with a default opaque color. To change that, and make the shape view act as a border, all we have to do is to apply a stroke to it, passing as argument the desired color. Text("Borders in SwiftUI!")


1 Answers

Demo

Demo


Implementation

You can use this modifier on any View:

.border(width: 5, edges: [.top, .leading], color: .yellow) 

With the help of this simple extension:

extension View {     func border(width: CGFloat, edges: [Edge], color: Color) -> some View {         overlay(EdgeBorder(width: width, edges: edges).foregroundColor(color))     } } 

And here is the magic struct behind this:

struct EdgeBorder: Shape {      var width: CGFloat     var edges: [Edge]      func path(in rect: CGRect) -> Path {         var path = Path()         for edge in edges {             var x: CGFloat {                 switch edge {                 case .top, .bottom, .leading: return rect.minX                 case .trailing: return rect.maxX - width                 }             }              var y: CGFloat {                 switch edge {                 case .top, .leading, .trailing: return rect.minY                 case .bottom: return rect.maxY - width                 }             }              var w: CGFloat {                 switch edge {                 case .top, .bottom: return rect.width                 case .leading, .trailing: return self.width                 }             }              var h: CGFloat {                 switch edge {                 case .top, .bottom: return self.width                 case .leading, .trailing: return rect.height                 }             }             path.addPath(Path(CGRect(x: x, y: y, width: w, height: h)))         }         return path     } } 
like image 135
Mojtaba Hosseini Avatar answered Sep 19 '22 14:09

Mojtaba Hosseini