I am trying to create a custom RGB CGColor
using Swift to use as a border color for a UIButton
. I've tried following code but the color is not visible:
var red = UIColor(red: 100.0, green: 130.0, blue: 230.0, alpha: 1.0)
self.layer.borderColor = red.CGColor
Is there any way to create a CGColor
directly from RGB values?
You have to give the values between 0 and 1.0. So divide the RGB values by 255.
Change Your code to
var red = UIColor(red: 100.0/255.0, green: 130.0/255.0, blue: 230.0/255.0, alpha: 1.0)
self.layer.borderColor = red.CGColor
您可以在 Swift 3 中将其缩减为一行:
avLayer.backgroundColor = UIColor.red.CGColor
On Mac OS X there is CGColorCreateGenericRGB()
, but it doesn't exist on iOS. You can use CGColorCreate
with appropriate parameters, though.
self.layer.borderColor = CGColorCreate(CGColorSpaceCreateDeviceRGB(), [1.0, 0.5, 0.5, 1.0])
The values in the array are, in order of appearance: red, green, blue, and alpha. Just like UIColor
:)
UImage *newImage = [self imageWithColor:[UIColor colorWithRed:200/255. green:228/255. blue:194/255. alpha:0.99]];
Maybe all you need is set the borderWidth
, by default it is 0, so it is not visible:
self.layer.borderWidth = 1.0
Just in case you can set the cornerRadius
if you need it rounded ... might be useful ;-)
self.layer.cornerRadius = 4.0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.