ios - 如何从 RGB 值中获取 CGColor?

标签 ios swift rgb uicolor cgcolor

我正在尝试使用 Swift 创建自定义 RGB CGColor 以用作 UIButton 的边框颜色。我尝试了以下代码,但颜色不可见:

var red = UIColor(red: 100.0, green: 130.0, blue: 230.0, alpha: 1.0)
self.layer.borderColor = red.CGColor

有什么方法可以直接从 RGB 值创建 CGColor 吗?

最佳答案

您必须给出 0 到 1.0 之间的值。所以将 RGB 值除以 255。

将您的代码更改为

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

关于ios - 如何从 RGB 值中获取 CGColor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29466582/

相关文章:

ios - 如何解析 "Initializer for conditional binding must have Optional type, not ' Int'”?

ios - 具有 View Controller 属性的台风

ios - swift 中单个对象的 NSNotification

android - 如何从 Android 上的位图中获取 RGB 值?

java - 坐标超出 .setRGB 范围

python - 用PIL保存后图像颜色发生变化

ios - 在 iOS 7 中用字母绘制实心圆圈

ios - Objective-C - iOS UITableView 底行删除时崩溃

ios - 我需要阅读哪个 swift 文档或视频才能了解此 ResultsHandler 的工作原理?

iphone - 从 TableView 推送到另一个 TableView 得到黑屏