DotPattern
is a Swift Package designed for SwiftUI applications, allowing you to add customizable dot patterns as backgrounds. This package is ideal for developers looking to maintain a consistent design language with dotted backgrounds across buttons, views, or other UI components.
Here’s an example of how to use DotPattern
in your SwiftUI views:
Below are some sample implementations of DotPattern
with different configurations:
private func dotBackground(dotColor: Color, backgroundColor: Color, dotSize: CGFloat, spacing: CGFloat, opacity: Double, cornerRadius: CGFloat) -> some View {
DotPattern(dotColor: dotColor, backgroundColor: backgroundColor, dotSize: dotSize, spacing: spacing, opacity: opacity)
.clipShape(RoundedRectangle(cornerRadius: cornerRadius))
}
// MARK: Dot Pattern Button
Button(action: {
print("Button pressed")
}) {
Text("Dot Pattern Button")
.foregroundColor(.white)
.padding()
.background(
dotBackground(dotColor: .white, backgroundColor: .red.opacity(0.8), dotSize: 3, spacing: 3, opacity: 0.2, cornerRadius: 12)
)
}
// MARK: Dot Pattern Button
// MARK: Card with Dot Pattern Overlay
RoundedRectangle(cornerRadius: 15)
.fill(Color.white)
.frame(height: 150)
.overlay(
dotBackground(dotColor: .gray, backgroundColor: .clear, dotSize: 4, spacing: 6, opacity: 0.1, cornerRadius: 15)
)
.shadow(radius: 5)
.padding()
.overlay(Text("Card with Dot Pattern").foregroundColor(.blue))
// MARK: Card with Dot Pattern Overlay
- Dot Color: Customize the color of the dots.
- Background Color: Set the background color behind the dots.
- Dot Size: Adjust the size of the dots.
- Spacing: Control the spacing between dots.
- Opacity: Adjust the opacity of the dots for subtle or more prominent patterns.
- iOS: 17.0 or newer
- Swift: 5.0 or newer
- SwiftUI
You can add DotPattern to your project using Swift Package Manager or by directly copying the code into your project.
- In Xcode, go to File > Add Packages.
- Enter the repository URL:
[https://github.com/acapellaonduty/DotPattern]
. - Select Add Package.
If you prefer to add DotPattern directly to your project, you can simply copy the code from the repository and include it in your project files. For better performance
- Copy the DotPattern.swift file.
- Paste it into your project folder in Xcode.
To improve performance when rendering a large number of dots, consider caching the pattern. Without caching, rendering too many dots dynamically might lead to performance issues. By pre-rendering the dot pattern into an image or a reusable layer, you can significantly reduce the computational load and ensure smoother performance.
For any questions or feedback, you can reach me at:
- Email: [email protected]
- GitHub: @imacapella
- LinkedIn: Gürkan Karadaş