Converting UIColor to CGColor in swift

67,440

Solution 1

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

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

Solution 2

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

Swift 4+ version

UIColor.lightGray.cgColor
Share:
67,440
Admin
Author by

Admin

Updated on July 08, 2022

Comments

  • Admin
    Admin almost 2 years

    This is the Obj-C code:

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

    How do I write it in swift.

  • Chris
    Chris over 9 years
    @idmean I don't think an explanation is needed. The question is basically asking for a single line of code. "Give me this Obj-C line in Swift". What kind of explanation are you expecting? This is all that's required to answer this kind of question
  • Chris
    Chris over 9 years
    That's why the question has 3 downvotes. Just because the OP doesn't know how to properly ask a question in english (perhaps there's a language barrier) doesn't mean he or she doesn't deserve an answer to the question. If no one could understand what was being asked, that's different. But it's easy to interpret what is being asked
  • Juan Boero
    Juan Boero over 8 years
    do not let bureaucracy interfere in the knowledge gain.