UIGradient - A simple and powerful library for using gradient layer, image, color

Overview



Platform Swift 5 Version License

Installation

UIGradient is now available on CocoaPods. Simply add the following to your project Podfile, and you'll be good to go.

pod 'UIGradient'

Usage

GradientLayer

Create a GradientLayer then add it to your view's

public extension UIView {

    func addGradientWithDirection(_ direction: GradientDirection, colors: [UIColor], cornerRadius: CGFloat = 0, locations: [Double]? = nil)

    func addGradient(_ gradientLayer: GradientLayer, cornerRadius: CGFloat = 0)
	
}

Example

let gradient = GradientLayer(direction: GradientDirection, colors: [UIColor])
view.addGradient(gradient)

UIGradient supports those directions below:

public enum GradientDirection {
    case topToBottom // top to bottom
    case bottomToTop // bottom to top
    case leftToRight // left to right
    case rightToLeft // right to left
    case topLeftToBottomRight // top left to bottom right
    case topRightToBottomLeft // top right to bottom left
    case bottomLeftToTopRight // bottom left to top right
    case bottomRightToTopLeft // bottom right to top left
}

You can also create a GradientLayer clone instance using this function:

open class GradientLayer: CAGradientLayer {
    ...
    public final func clone() -> GradientLayer
    ...
}

Gradient color

UIGradient allows you create gradient color

public extension UIColor {

    static func fromGradient(_ gradient: GradientLayer, frame: CGRect, cornerRadius: CGFloat = 0) -> UIColor?
	
    static func fromGradientWithDirection(_ direction: GradientDirection, frame: CGRect, colors: [UIColor], cornerRadius: CGFloat = 0, locations: [Double]? = nil) -> UIColor?
	
}

Example

let view1 = UIView(frame: frame)
view1.backgroundColor = UIColor.fromGradientWithDirection(.topToBottom, frame: frame, colors: [UIColor.blue, UIColor.green])
//view1.backgroundColor = UIColor.fromGradient(gradient, frame: frame)
let label = UILabel(frame: frame)
label.text = "some text"
label.textColor = UIColor.fromGradient(gradient, frame: frame)

Gradient image

UIGradient also allows you create gradient image

public extension UIImage {

    static func fromGradient(_ gradient: GradientLayer, frame: CGRect, cornerRadius: CGFloat = 0) -> UIImage?
	
    static func fromGradientWithDirection(_ direction: GradientDirection, frame: CGRect, colors: [UIColor], cornerRadius: CGFloat = 0, locations: [Double]? = nil) -> UIImage?
	
}

Example

let imageView = UIImageView(frame: frame)
imageView.image = UIImage.fromGradientWithDirection(.bottomLeftToTopRight, frame: frame, colors: [UIColor.blue, UIColor.green])

Gradient library

UIGradient was inspired by @lukedesigner. I created 24 gradients based on his website and make them static variables of GradientLayer so everyone can use those gradients for their projects.

let oceanBlueGradient 		= GradientLayer.oceanBlue
let sanguineGradient  		= GradientLayer.sanguine
let lusciousLimeGradient 	= GradientLayer.lusciousLime
let purpleLakeGradient 		= GradientLayer.purpleLake
...

You can explore them in my example project.

Author

UIGradient was created by Đinh Quang Hiếu ([email protected]) in 2017 using Swift. If you found any problem please create issue or pull request to help me improve this open source project. Thanks!

License

UIGradient is released and distributed under the terms and conditions of the MIT license.

You might also like...
HEX color handling as an extension for UIColor. Written in Swift.

SwiftHEXColors HEX color handling as an extension for UIColor. Written in Swift.

A tool to calculate the color ratio of UIImage in iOS.
A tool to calculate the color ratio of UIImage in iOS.

UIImageColorRatio A tool to calculate the color ratio of UIImage in iOS. How to use UIImageColorRatio Get the color ratio of UIImage. let image = ...

An attractive color generator for Swift. Ported from randomColor.js.
An attractive color generator for Swift. Ported from randomColor.js.

Random Color Swift Inspired by David Merfield's randomColor.js. It is a ported version to Swift. You can use the library to generate attractive random

Flat UI color palette helpers written in Swift.
Flat UI color palette helpers written in Swift.

FlatUIColors (swift) install Make sure you have the latest version of CocoaPods (gem install cocoapods) that has Swift support. At the time of this wr

A UIColor extension with CSS3 Color names.
A UIColor extension with CSS3 Color names.

CSS3ColorsSwift Overview CSS3ColorsSwift provides a UIColor extension with Web Color names. Demo Run the demo project in the Demo directory without ca

Google Material Color Palette in Swift

Google Material Color in Swift Defined Google Material Color value ready to use in Swift refer to Google Material Design in Style/Color section ###How

Generate color based on the given string.
Generate color based on the given string.

PFColorHash Swift 4.0 SUPPORT! Generate color based on the given string. Thanks to color-hash. Usage Basic let colorHash = PFColorHash() // in HSL, H

Aesthetic color-scheme generation written in Swift
Aesthetic color-scheme generation written in Swift

Lorikeet Lightweight framework for generating visually aesthetic color-schemes in Swift Requirements UIKit Features What can Lorikeet do for you Calcu

Overrides macOS external display EDIDs to force RGB color

edidiotic edidiotic creates EDID overrides for external displays on macOS that set the display type to RGB color and removes all extension blocks. Thi

Comments
  • Add support for custom gradient locations + custom angle

    Add support for custom gradient locations + custom angle

    Hi @dqhieu

    this is a great little library. I extended it to support custom gradient break locations and supporting custom angles in degrees.

    Please merge!

    opened by rolandkakonyi 1
  • UIGradient 1.4

    UIGradient 1.4

    Project board: https://github.com/dqhieu/UIGradient/projects/1

    Feature request

    • [ ] Support adding border gradient

    Changes

    • GradientDirection -> GradientLayer.Direction
    • Make direction property public (GradientLayer)
    • Add uiColors: [UIColor] property (GradientLayer)
    opened by dqhieu 1
Releases(1.3.2)
Owner
Đinh Quang Hiếu
Đinh Quang Hiếu
A powerful and easy to use live mesh gradient renderer for iOS.

MeshKit A powerful and easy to use live mesh gradient renderer for iOS. This project wouldn't be possible without the awesome work from Moving Parts a

Ethan Lipnik 51 Jan 1, 2023
System Color Picker - The macOS color picker as an app with more features

System Color Picker The macOS color picker as an app with more features Download Requires macOS 11 or later. Features Quickly copy, paste, and convert

Sindre Sorhus 758 Dec 24, 2022
ColorWheel Test - An attempt at creating a Color Wheel to be utilized for color picking in SwiftUI utlizing various tutuorials on youtube

This code focuses on creating a Color Wheel in which a user will be able to select a desired color to then be implemented in another project that will display that color in an LED connected to an arduino

Gerardo Cerpa 0 Jan 15, 2022
A Lightweight But Powerful Color Kit (Swift)

BCColor A lightweight but powerful color kit (Swift) Features Pick Colors From Image Generate Monochrome Image Support Hex Color Style Lighten / Darke

Xiaobo Zhang 418 Nov 25, 2022
Conical (angular) gradient for iOS written in Swift

AEConicalGradient Conical (angular) gradient in Swift I hope that somebody will find this useful. And nice. Usage AEConicalGradient is a minion which

Marko Tadić 82 Dec 27, 2022
Convenience methods for creating color using RGBA hex string.

UIColor+Hex, now Swift. Convenience method for creating autoreleased color using RGBA hex string.

R0CKSTAR 1.2k Dec 23, 2022
A simple UIColor category to get color with hex code.

TFTColor A simple UIColor library to get UIColor object from RGB hex string/value, CMYK hex string/value or CMYK base component values. You can also r

Burhanuddin Sunelwala 18 Jun 21, 2022
A beautiful set of predefined colors and a set of color methods to make your iOS/OSX development life easier.

Installation Drag the included Colours.h and Colours.m files into your project. They are located in the top-level directory. You can see a demo of how

Ben Gordon 3.1k Dec 28, 2022
ChromaColorPicker - An intuitive HSB color picker built in Swift

An intuitive HSB color picker built in Swift. Supports multiple selection handles and is customizable to your needs.

Jonathan Cardasis 536 Dec 29, 2022
SheetyColors is an action sheet styled color picker for iOS

?? Based on UIAlertController: The SheetyColors API is based on UIKit's UIAlertController. Simply add buttons to it as you would for any other Action Sheet by defining UIAlertAction instances. Therefore, it nicely integrates with the look & feel of all other native system dialogs. However, you can also chose to use the color picker it self without an action sheet.

Christoph Wendt 102 Nov 10, 2022