简体   繁体   English

在Swift中的绘图类中设置自定义RGB颜色

[英]Setting custom RGB color in drawing class in Swift

In the below simplified drawing function, I am able to set the alpha of a drawing stroke. 在下面的简化绘图功能中,我可以设置绘图笔触的Alpha。

I have a custom color with RGB values, for example Red = 0.25, Green = 0.5, Blue = 0.75. 我有一个具有RGB值的自定义颜色,例如Red = 0.25,Green = 0.5,Blue = 0.75。

How do I set a custom RGB stroke color in the drawing function similar to setting the alpha? 如何在绘图功能中设置自定义RGB笔触颜色,类似于设置alpha?

Is it also possible to set a stroke color together with setting the stroke alpha in a single statement? 是否可以在单个语句中同时设置笔触颜色和设置笔触alpha?

override func drawRect(rect: CGRect) {

strokeColor.colorWithAlphaComponent(0.5).setStroke()
path?.stroke()

}

If you want to set the color try this: 如果要设置颜色,请尝试以下操作:

var Color = UIColor(red: 200.0/255.0, green: 16.0/255.0, blue: 46.0/255.0, alpha: 1.0)

also a very useful tool/ website for converting the colors into useful Swift code: 也是一个非常有用的工具/网站,用于将颜色转换为有用的Swift代码:

https://www.ralfebert.de/snippets/ios/swift-uicolor-picker/ https://www.ralfebert.de/snippets/ios/swift-uicolor-picker/

Edit: 编辑:

self.button.setTitleColor(UIColor(red:0.96, green:0.28, blue:0.28, alpha:1.0), forState: .Normal)

or if you want a cleaner solution: 或者,如果您需要更清洁的解决方案:

var myColor = UIColor(red:0.96, green:0.28, blue:0.28, alpha:1.0)
self.button.setTitleColor(myColor, forState: .Normal)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM