How do you use the appropriate color class for the current platform?
Use conditional compilation and type aliases:
#if os(OSX)
typealias Color = NSColor
typealias Image = NSImage
#else
typealias Color = UIColor
typealias Image = UIImage
#endif
Then use Color instead of UIColor or NSColor:
self.gameView!.backgroundColor = Color(red: 0, green: 0.2, blue: 0.5, alpha: 1)
Edit 2016-01-17: As DDPWNAGE noted above, Apple has created SKColor
with basically the same definition.
Universal class for Mac OS and iOS
You should be able to use typealias
for the color class, like this:
#if os(iOS)
typealias XColor = UIColor
#elseif os(OSX)
typealias XColor = NSColor
#endif
func myFunc(color: XColor?) {
self.myFuncX(color)
}
The idea is to limit conditional compile to a type definition for XColor
, and then using that type alias in place of UIColor
or NSColor
, as required on the particular system.
how to have the color of the windows theme MAUI
UISettings Class could be used to get current system color in windows platform , the point is how to convert Windows.UI.Color
to Microsoft.Maui.Graphics.Color
.
Sample code
#if WINDOWS
var uiSettings = new Windows.UI.ViewManagement.UISettings();
var color = uiSettings.GetColorValue(UIColorType.Accent);
//change button color with system theme color
button.BackgroundColor = Microsoft.Maui.Graphics.Color.Parse(color.ToString());
#endif
And if you want to change the color dynamically according to system theme, take a look at Application.Current.RequestedThemeChanged event ,it is used to to detect the change of system theme .
Notice: The following event is only triggered when switching between light/dark theme .
Sample code
Application.Current.RequestedThemeChanged += (s, a) =>
{
#if WINDOWS
var uiSettings = new Windows.UI.ViewManagement.UISettings();
var color = uiSettings.GetColorValue(UIColorType.Accent);
//change button color with system theme color
button.BackgroundColor = Microsoft.Maui.Graphics.Color.Parse(color.ToString());
#endif
};
Use a parent class init inside of a class protocol that conforms to a class
Here's how I would do it. No protocol needed! Just extend SKSpriteNode and inherit:
extension UIColor {
static var image : UIColor {
UIColor(red: 1, green: 1, blue: 1, alpha: 0)
}
}
extension SKTexture {
convenience init(_ s:String) {
self.init(imageNamed:s)
}
}
extension SKSpriteNode {
@objc class var size: CGSize { .zero } // override me for subclass
convenience init(_ name: String) {
self.init(texture: SKTexture(name), color: .image, size: Self.size)
}
}
// example
class Platform: SKSpriteNode {
override class var size: CGSize {
CGSize(width: 300, height: 50)
}
}
And now you are able to say let p = Platform("yoho")
, which is what I believe we were aiming at.
Creating an Array of CGColors from an Array of UIColors or NSColors
You could do:
extension _ArrayType where Generator.Element == UIColor {
func CGColors() -> [CGColorRef] {
var cgColorArray: [CGColorRef] = []
for color in self {
cgColorArray.append(color.CGColor)
}
return cgColorArray
}
}
Use it like so:
let colors: [UIColor] = [UIColor.redColor(), UIColor.orangeColor()]
colors.CGColors()
Repeat for NSColor.
How can I get color-int from color resource?
You can use:
getResources().getColor(R.color.idname);
Check here on how to define custom colors:
http://sree.cc/google/android/defining-custom-colors-using-xml-in-android
EDIT(1):
Since getColor(int id)
is deprecated now, this must be used :
ContextCompat.getColor(context, R.color.your_color);
(added in support library 23)
EDIT(2):
Below code can be used for both pre and post Marshmallow (API 23)
ResourcesCompat.getColor(getResources(), R.color.your_color, null); //without theme
ResourcesCompat.getColor(getResources(), R.color.your_color, your_theme); //with theme
Related Topics
How to Check Two Instances Are the Same Class/Type in Swift
Should Conditional Compilation Be Used to Cope With Difference in Cgfloat on Different Architectures
How to Load an Image from Documents Directory on MACos Swift
Hide Navigation Bar Without Losing Swipe Back Gesture in Swiftui
Swift: Determine What Object Called a Function
Arkit: Placing an Scntext at a Particular Point in Front of the Camera
Swift Setter Causing Exc_Bad_Access
Firebase Query Ordering Not Working Properly
How to Check Is a String or Number
Convert Uiimage to Grayscale Keeping Image Quality
Spritekit Physics in Swift - Ball Slides Against Wall Instead of Reflecting
Swift: How to Detect Linear Type Barcodes
Compiler Error When Comparing Values of Enum Type with Associated Values
Swift Protocol Error: 'Weak' Cannot Be Applied to Non-Class Type
How We Can Get and Read Size of a Text with Geometryreader in Swiftui