yeahdongcn/UIColor-Hex-Swift

Support MacOS

clearbrian opened this issue · 2 comments

Can you add support for MacOS
You can do it easily with an typealias

I've had to import it to my Mac Project from source rather than cocoapods. Would prefer Cocoapods.

A quick way to support
macos
Just replace top of
UIColorExtension.swift

import UIKit
@objc extension UIColor {

WITH


#if os(iOS)
//---------------------------
//iOS
//---------------------------
import Foundation
import UIKit
typealias AppColor = UIColor

//---------------------------
#elseif os(macOS)
//---------------------------
//macOS
//---------------------------
import Cocoa
typealias AppColor = NSColor

//---------------------------
//#else
// os(tvOS) || os(watchOS)
//---------------------------
#endif


@objc extension AppColor {
    /**
     The shorthand three-digit hexadecimal representation of color.
     #RGB defines to the color #RRGGBB.
     
     - parameter hex3: Three-digit hexadecimal value.
     - parameter alpha: 0.0 - 1.0. The default is 1.0.
     */
    public convenience init(hex3: UInt16, alpha: CGFloat = 1) {
        let divisor = CGFloat(15)
        let red     = CGFloat((hex3 & 0xF00) >> 8) / divisor
        let green   = CGFloat((hex3 & 0x0F0) >> 4) / divisor
        let blue    = CGFloat( hex3 & 0x00F      ) / divisor
        self.init(red: red, green: green, blue: blue, alpha: alpha)
    }
...}

Swift 5 was throwing errors about failable inits and non failable in
I had to change
init

   public convenience init(_ rgba: String, defaultColor: NSColor = NSColor.clear) {

TO init?

public convenience init?(_ rgba: String, defaultColor: NSColor = NSColor.clear) {

Could you please create a PR? I can test it tomorrow morning.

use_frameworks!

pod 'UIColor_Hex_Swift', '~> 5.1.0'