Skip to content

nissaba/EFColorPicker

 
 

Repository files navigation

EFColorPicker is a lightweight color picker in Swift, inspired by MSColorPicker.

中文介绍

Overview

Color picker component for iOS. It allows the user to select a color with color components. Key features:

  • iPhone & iPad support
  • Adaptive User Interface
  • Supports RGB and HSB color models
  • Well-documented
  • Compatible with iOS 8.0 (iPhone & iPad) and higher

Preview

iPhone iPad

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Then build and run EFColorPicker.xcworkspace in Xcode, the demo shows how to use and integrate the EFColorPicker into your project.

Requirements

  • iOS 8.0+
  • Xcode 9.0+
  • Swift 4.0+

Installation

EFColorPicker is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'EFColorPicker'

Use

  1. First, include EFColorPicker in your project:
import EFColorPicker
  1. Next, we can call EFColorPicker with pure code:
let colorSelectionController = EFColorSelectionViewController()
let navCtrl = UINavigationController(rootViewController: colorSelectionController)
navCtrl.navigationBar.backgroundColor = UIColor.white
navCtrl.navigationBar.isTranslucent = false
navCtrl.modalPresentationStyle = UIModalPresentationStyle.popover
navCtrl.popoverPresentationController?.delegate = self
navCtrl.popoverPresentationController?.sourceView = sender
navCtrl.popoverPresentationController?.sourceRect = sender.bounds
navCtrl.preferredContentSize = colorSelectionController.view.systemLayoutSizeFitting(
    UILayoutFittingCompressedSize
)

colorSelectionController.delegate = self
colorSelectionController.color = self.view.backgroundColor ?? UIColor.white

if UIUserInterfaceSizeClass.compact == self.traitCollection.horizontalSizeClass {
    let doneBtn: UIBarButtonItem = UIBarButtonItem(
        title: NSLocalizedString("Done", comment: ""),
        style: UIBarButtonItemStyle.done,
        target: self,
        action: #selector(ef_dismissViewController(sender:))
    )
    colorSelectionController.navigationItem.rightBarButtonItem = doneBtn
}
self.present(navCtrl, animated: true, completion: nil)

Also we can use EFColorPicker in Storyboard:

if "showPopover" == segue.identifier {
	guard let destNav: UINavigationController = segue.destination as? UINavigationController else {
	    return
	}
	if let size = destNav.visibleViewController?.view.systemLayoutSizeFitting(UILayoutFittingCompressedSize) {
	    destNav.preferredContentSize = size
	}
	destNav.popoverPresentationController?.delegate = self
	if let colorSelectionController = destNav.visibleViewController as? EFColorSelectionViewController {
	    colorSelectionController.delegate = self
	    colorSelectionController.color = self.view.backgroundColor ?? UIColor.white

	    if UIUserInterfaceSizeClass.compact == self.traitCollection.horizontalSizeClass {
	        let doneBtn: UIBarButtonItem = UIBarButtonItem(
	            title: NSLocalizedString("Done", comment: ""),
	            style: UIBarButtonItemStyle.done,
	            target: self,
	            action: #selector(ef_dismissViewController(sender:))
	        )
	        colorSelectionController.navigationItem.rightBarButtonItem = doneBtn
	    }
	}
}

You can control the visibility of color textField by change the isColorTextFieldHidden property of EFColorSelectionViewController, for example:

isColorTextFieldHidden: true isColorTextFieldHidden: false

For more detail, please see the demo.

  1. Last but not the least, you should implement EFColorSelectionViewControllerDelegate so you can sense the color changes:
// MARK:- EFColorSelectionViewControllerDelegate
func colorViewController(colorViewCntroller: EFColorSelectionViewController, didChangeColor color: UIColor) {
    self.view.backgroundColor = color

    // TODO: You can do something here when color changed.
    print("New color: " + color.debugDescription)
}

PS

The first version of EFColorPicker is converted from MSColorPicker, thanks for sgl0v's work!

Author

EyreFree, [email protected]

License

EFColorPicker is available under the MIT license. See the LICENSE file for more info.

About

A lightweight color picker in Swift.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 98.4%
  • Ruby 1.6%