Converting UIColor to CGColor in swift

Objective CSwiftColorsUicolor

Objective C Problem Overview


This is the Obj-C code:

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

How do I write it in swift.

Objective C Solutions


Solution 1 - Objective C

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

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

Solution 2 - Objective C

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!

Solution 3 - Objective C

Swift 4+ version

UIColor.lightGray.cgColor

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShakti ChauhanView Question on Stackoverflow
Solution 1 - Objective COnik IVView Answer on Stackoverflow
Solution 2 - Objective CGeorge D GirtonView Answer on Stackoverflow
Solution 3 - Objective Cyassine menssiView Answer on Stackoverflow