https://github.com/chenhaiteng/gradientbuilder
A custom result builder to help build gradient easily.
https://github.com/chenhaiteng/gradientbuilder
color gradient-color resultbuilder swift swiftui
Last synced: 2 months ago
JSON representation
A custom result builder to help build gradient easily.
- Host: GitHub
- URL: https://github.com/chenhaiteng/gradientbuilder
- Owner: chenhaiteng
- License: mit
- Created: 2021-08-13T14:27:57.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2024-10-04T07:32:24.000Z (8 months ago)
- Last Synced: 2025-03-14T09:47:40.387Z (3 months ago)
- Topics: color, gradient-color, resultbuilder, swift, swiftui
- Language: Swift
- Homepage:
- Size: 35.2 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# GradientBuilder
For design detail, read [Go through swift result builders](https://chenhaiteng.github.io/swift/2021/08/16/go-through-swift-result-builders.html)
## Installation:
### CocoaPods
GradientBuilder is now available through [CocoaPods](https://cocoapods.org). To install
it, simply add the following line to your Podfile:```ruby
pod 'GradientBuilder'
```### Swift Package Manager
GradientBuilder is published with Swift Package Manager, you can get more information at , , and #### - Add to Xcode(To use this package in your application):
1. File > Swift Packages > Add Package Dependency...
2. Choose Project you want to add GradientBuilder
3. Paste repository https://github.com/chenhaiteng/GradientBuilder.git
4. Rules > Version: Up to Next Major 1.0.0
It's can also apply Rules > Branch : main to access the latest code.**Note:** It might need to link GradientBuilder to your target maunally.
1. Open *Project Editor* by tap on root of project navigator
2. Choose the target you want to use GradientBuilder.
3. Choose **Build Phases**, and expand **Link Binary With Libraries**
4. Tap on **+** button, and choose GradientBuilder to add it.#### - Add to SPM package(To use this package in your library/framework):
```swift
dependencies: [
.package(name: "GradientBuilder", url: " https://github.com/chenhaiteng/GradientBuilder.git", from: "1.0.0")
// To specify branch, use following statement to instead of.
// .package(name: "GradientBuilder", url: " https://github.com/chenhaiteng/GradientBuilder.git", .branch("branch_name"))
],
targets: [
.target(
name: "MyPackage",
dependencies: ["GradientBuilder"]),
]
```
---
## How to use1. apply on function
```swift
// Create a Gradient, then inject it into specified gradient shape.
@GradientBuilder
func builder() -> Gradient {
Color.red
Color.blue
Color.yellow
}let g = builder()
let angularGradient = AngularGradient(gradient: g, center: .center)// Or create gradient shape directly
@GradientBuilder
func angularBuilder() -> AngularGradient {
Color.red
Color.blue
Color.yellow
}let angular = angularBuilder()
```2. apply on a arameter in a function declaration
```swift
func createGradient(@GradientBuilder builder: ()->Gradient) -> Gradient {
return builder()
}let g = createGradient {
Color.red
Color.blue
Color.yellow
}
```3. apply on a variable or subscript
```swift
enum GrayGradient {
@GradientBuilder static subscript(_ degree: Int) -> LinearGradient {
if degree > 0 {
for i in 0..