1

I am creating a UILabel that will have a gradient background color. The only problem is I am getting an error message saying the following:

"Cannot covert value of type Int to expected argument type CGGradientDrawingOptions"

I am wondering if this is because of a simple syntax error or if I need to add or delete something from my code. Please inform me on what I need to add, delete, or fix in your answer and what the error means. Here is all the code:

import UIKit

@IBDesignable class PHLabel: UILabel {

@IBInspectable var startColor: UIColor = UIColor.greenColor()
@IBInspectable var endColor: UIColor = UIColor.greenColor()

override func drawRect(rect: CGRect) {

let context = UIGraphicsGetCurrentContext()
let colors = [startColor.CGColor, endColor.CGColor]

let colorSpace = CGColorSpaceCreateDeviceRGB()

let colorLocations:[CGFloat] = [0.0, 1.0]

let gradient = CGGradientCreateWithColors(colorSpace, colors, colorLocations)

var startPoint = CGPoint.zero
var endPoint = CGPoint(x:0, y:self.bounds.height)
    CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0)
}
}

Any suggestions or input is greatly appreciated.

Thanks in advance.

Bigfoot11
  • 911
  • 2
  • 11
  • 25
  • The first Google hit for "CGContextDrawLinearGradient swift 2" is https://forums.developer.apple.com/thread/17470, and there `CGGradientDrawingOptions(rawValue: 0)` or simply `[]` is suggested as solution for the options argument. See also http://stackoverflow.com/questions/24066170/how-to-create-ns-options-style-bitmask-enumerations-in-swift. – Martin R Jan 26 '16 at 23:17

2 Answers2

2

CGGradientDrawingOptions is an OptionSetType and cannot be implicitly cast from an Int (since Swift 2).

struct CGGradientDrawingOptions : OptionSetType {
      init(rawValue rawValue: UInt32)
      static var DrawsBeforeStartLocation: CGGradientDrawingOptions { get }
      static var DrawsAfterEndLocation: CGGradientDrawingOptions { get }
} 

In your case the zero value is [ ]. If you want to use options, you can type something like this :

let opts: CGGradientDrawingOptions = [
    .DrawsBeforeStartLocation,
    .DrawsAfterEndLocation
]
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, opts)

As Martin.R. said above you can also use CGGradientDrawingOptions(rawValue: 0) but it is not appropriate in your case.

Jeremy Vizzini
  • 261
  • 1
  • 9
  • Is there a simple fix that I can use in place of the 0? – Bigfoot11 Jan 26 '16 at 23:25
  • 2
    You can replace 0 by []. CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, []) – Jeremy Vizzini Jan 26 '16 at 23:27
  • It worked, thanks. But just one more question, how do I add text to this label? – Bigfoot11 Jan 26 '16 at 23:31
  • The UILabel has a text attribute. You have to draw this string in the drawRect method. Something as (self.string as? NSString)?.drawInRect ... You can also use the attributedText attribute instead of the text attribute. – Jeremy Vizzini Jan 26 '16 at 23:37
  • I made a mistake. It is not (self.string as? NSString) but (self.text as? NSString) All methods to draw a NSString are listed here : https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/NSString_UIKit_Additions/index.html – Jeremy Vizzini Jan 26 '16 at 23:46
0

SWIFT 2.0

Use as below in swift 2. jsut type the dot and select the option. CGContextDrawLinearGradient(context, gradient, startPoint, endPoint,.DrawsAfterEndLocation)

Chamath Jeevan
  • 5,072
  • 1
  • 24
  • 27