Last active
July 13, 2018 15:00
-
-
Save popmedic/75f6213787716b2f7e029c8ea9a3803c to your computer and use it in GitHub Desktop.
ColorPickerTextField - Use ColorPickerTextField when you want to show a list of colors in a UIPickerView when editing text field.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
public class ColorPickerTextField: PickerTextField { | |
public var colorChoices = [ | |
UIColor.blackColor(), | |
UIColor.blueColor(), | |
UIColor.brownColor(), | |
UIColor.darkGrayColor(), | |
UIColor.cyanColor(), | |
UIColor.grayColor(), | |
UIColor.greenColor(), | |
UIColor.lightGrayColor(), | |
UIColor.magentaColor(), | |
UIColor.orangeColor(), | |
UIColor.purpleColor(), | |
UIColor.redColor(), | |
UIColor.whiteColor(), | |
UIColor.yellowColor()] | |
public required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
self.choices = [ | |
"Black", | |
"Blue", | |
"Brown", | |
"Dark Gray", | |
"Cyan", | |
"Gray", | |
"Green", | |
"Light Gray", | |
"Magenta", | |
"Orange", | |
"Purple", | |
"Red", | |
"White", | |
"Yellow"] | |
self.text = choices[0] | |
self.layer.shadowRadius = 1.0 | |
self.onSelected = self._onSelected | |
} | |
public var onColorSelected:((color:UIColor)->Void)? = nil | |
public func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat { | |
return 44.0 | |
} | |
public func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView { | |
if let timer = self._selectedTimer { | |
timer.invalidate() | |
self._selectedTimer = nil | |
} | |
let frame = CGRect(x: 0, y: 0, width: pickerView.frame.size.width, height: 44.0) | |
var aView:UIView | |
if let view = view { | |
aView = view | |
} | |
else { | |
aView = UIView(frame:frame) | |
} | |
aView.backgroundColor = colorChoices[row] | |
var red:CGFloat = 0.0 | |
var green:CGFloat = 0.0 | |
var blue:CGFloat = 0.0 | |
var alpha:CGFloat = 0.0 | |
aView.backgroundColor?.getRed(&red, green: &green, blue: &blue, alpha: &alpha) | |
let label = UILabel(frame: frame) | |
label.attributedText = NSAttributedString(string: self.choices[row], attributes: [NSForegroundColorAttributeName:UIColor(red: 1.0 - red, green: (1.0 - green), blue:(1.0 - blue), alpha:1.0)]) | |
label.textAlignment = NSTextAlignment.Center | |
aView.addSubview(label) | |
return aView | |
} | |
private func _onSelected(row: Int) { | |
self.backgroundColor = self.colorChoices[row] | |
var red:CGFloat = 0.0 | |
var green:CGFloat = 0.0 | |
var blue:CGFloat = 0.0 | |
var alpha:CGFloat = 0.0 | |
self.backgroundColor?.getRed(&red, green: &green, blue: &blue, alpha: &alpha) | |
self.textColor = UIColor(red: 1.0 - red, green: 1.0 - green, blue: 1.0 - blue, alpha: 1.0) | |
if let selected = self.onColorSelected { | |
selected(color:self.colorChoices[row]) | |
} | |
} | |
public var currentColor:UIColor { | |
get { | |
return self.colorChoices[self.currentRow] | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment