An UILabel subclass that uses gradients as its text or background color.
JNGradientLabel is available through CocoaPods, Carthage and the Swift Package Manager.
To install via CocoaPods, simply add the following line to your Podfile:
pod 'JNGradientLabel'
To install via Carthage, simply add the following line to your Cartfile:
github "SomeRandomiOSDev/JNGradientLabel"
To install via the Swift Package Manager add the following line to your Package.swift
file's dependencies
:
.package(url: "https://github.com/SomeRandomiOSDev/JNGradientLabel.git", from: "1.0.0")
First import JNGradientLabel at the top of your source file:
Swift:
import JNGradientLabel
Objective-C:
@import JNGradientLabel;
After initializing the label (from a Storyboard/XIB or manually) call one of following methods:
Swift:
// To produce an `Axial` (Linear) gradient
label.setAxialGradientParameters(startPoint: startPoint,
endPoint: endPoint,
colors: gradientColors,
locations: gradientlocations,
options: ... /* Optional CGGradientDrawingOptions parameter */)
OR
// To produce a `Radial` gradient
label.setRadialGradientParameters(startCenter: startCenter,
startRadius: startRadius,
endCenter: endCenter,
endRadius: endRadius,
colors: gradientColors,
locations: gradientlocations,
radiiScalingRule: ... /* Optional RadialGradientRadiiScalingRule parameter */,
options: ... /* Optional CGGradientDrawingOptions parameter */)
Objective-C:
// To produce an `Axial` (Linear) gradient
[label setAxialGradientParametersWithStartPoint:startPoint
endPoint:endPoint
colors:gradientColors
locations:gradientLocations
options:gradientDrawingOptions];
OR
// To produce a `Radial` gradient
[label setRadialGradientParametersWithStartCenter:startCenter
startRadius:startRadius
endCenter:endCenter
endRadius:endRadius
colors:gradientColors
locations:gradientLocations
radiiScalingRule:radiiScalingRule
options:gradientDrawingOptions];
And thats it!
By default, the label draws the gradient as the color of the text. The label also supports drawing the gradient as the background of the text. To do this, simply set the textGradientLocation
property as follows:
Swift:
label.textGradientLocation = .background
Objective-C:
label.textGradientLocation = TextGradientLocationBackground;
OR
[label setTextGradientLocation:TextGradientLocationBackground];
If you have need for a specific feature or you encounter a bug, please open an issue. If you extend the functionality of JNGradientLabel yourself or you feel like fixing a bug yourself, please submit a pull request.
Joe Newton, somerandomiosdev@gmail.com
JNGradientLabel is available under the MIT license. See the LICENSE
file for more info.