Shakti Chauhan
Shakti Chauhan

Reputation: 1091

Converting UIColor to CGColor in swift

This is the Obj-C code:

CGContextSetStrokeColorWithColor(context, [[UIColor lightGrayColor] CGColor]);

How do I write it in swift.

Upvotes: 104

Views: 78440

Answers (3)

yassine menssi
yassine menssi

Reputation: 373

Swift 4+ version

UIColor.lightGray.cgColor

Upvotes: 6

Onik IV
Onik IV

Reputation: 5047

// Original answer.
var newColor = UIColor.lightGrayColor().CGColor

// Swift 3 version
var newColor = UIColor.lightGray.cgColor

Upvotes: 301

George D Girton
George D Girton

Reputation: 813

Oddly enough, as Swift has evolved to 3.0 the answer has evolved as well. I ran across this while converting some Objective C code to Swift. I hope this helps someone! :-) Here is what it is today:

context.setStrokeColor(UIColor.lightGray.cgColor)

And it's funny, this "light gray" answer is actually helping me debug some code too! Thank iou!

Upvotes: 5

Related Questions