快速转换 UIColor 到 CGColor

这是 Obj-C 密码:

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

我怎样才能快速地写出来。

72171 次浏览
// Original answer.
var newColor = UIColor.lightGrayColor().CGColor


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

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!

Swift 4+ version

UIColor.lightGray.cgColor