Skip to content

Instantly share code, notes, and snippets.

@verec
Last active January 9, 2017 22:58
Show Gist options
  • Save verec/238ee65968321c0e78a3 to your computer and use it in GitHub Desktop.
Save verec/238ee65968321c0e78a3 to your computer and use it in GitHub Desktop.
import CoreGraphics
import Foundation
import UIKit
class GradientLabel : UILabel {
let gradient: CGGradient!
let alignedToSuperview: Bool!
init(colors: [UIColor]!, locations: [CGFloat]!, alignedToSuperview: Bool = true) {
super.init(frame: CGRect.zeroRect)
let gradientColors = colors.map {(color: UIColor!) -> AnyObject! in return color.CGColor as AnyObject! } as NSArray
let colorSpace = CGColorSpaceCreateDeviceRGB()
self.gradient = CGGradientCreateWithColors(colorSpace, gradientColors, locations)
self.alignedToSuperview = alignedToSuperview
}
override func drawTextInRect(rect: CGRect) {
let context = UIGraphicsGetCurrentContext()
// create the mask by drawing in the context first
CGContextSaveGState(context) ;
CGContextSetTextDrawingMode(context, kCGTextFill) ;
super.drawTextInRect(rect) ;
let lines = CGRect (origin: CGPoint(x: 0, y:16.0), size: CGSize(width: 160.0, height: 2.0)) ;
CGContextSetFillColorWithColor(context, UIColor.whiteColor().CGColor) ;
CGContextFillRect(context, lines) ;
// snapshot the context into the just created mask
let alphaMask = CGBitmapContextCreateImage(context) ;
CGContextClearRect(context, rect) ;
CGContextTranslateCTM(context, 0, rect.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// Clip the current context to our alphaMask
CGContextClipToMask(context, rect, alphaMask);
let startPoint = CGPoint(x:0.0, y: 0.0)
let endPoint = CGPoint(x:self.superview.bounds.size.width, y: 0.0)
if self.alignedToSuperview {
CGContextTranslateCTM(context, -1.0 * self.frame.origin.x, 0.0)
}
// As of Xcode 6 beta 4, CGGradientDrawingOptions does not play well with Int and UInt32 confusion ...
// var options: CGGradientDrawingOptions = kCGGradientDrawsBeforeStartLocation as CGGradientDrawingOptions
// | kCGGradientDrawsAfterEndLocation GGradientDrawsBeforeStartLocation as UInt32 ;
// CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, options) ;
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 3)
CGContextRestoreGState(context)
}
}
sample use:
self.grLabel2 = GradientLabel(colors:[UIColor.whiteColor(), UIColor.greenColor(), UIColor.orangeColor()], locations:[0.0, 0.75, 1.0])
self.grLabel2.textColor = TEXT_GRADIENT_COLOR ;
self.grLabel2.font = TOOLBAR_TITLE_FONT
self.grLabel2.text = "Swift Gradient"
self.grLabel2.sizeToFit()
self.grLabel2.frame = self.grLabel2.frame.rectByCenteringXIntoRect(UIScreen.mainScreen().bounds)
self.grLabel2.frame.origin.y += 20.0
self.addSubview(self.grLabel2)
using:
extension CGRect {
var center : CGPoint {
return CGPoint(
x: self.origin.x + (self.size.width / 2.0)
, y: self.origin.y + (self.size.height / 2.0)
)
}
func rectByCenteringIntoRect(intoRect: CGRect) -> CGRect {
var rect = self
rect.origin.x = (intoRect.size.width - rect.size.width) / 2.0
rect.origin.y = (intoRect.size.height - rect.size.height) / 2.0
return rect
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment