Created
July 14, 2015 23:02
-
-
Save higepon/ba1af2e19e8ffcdb7f2a to your computer and use it in GitHub Desktop.
Example of @IBDesignable without overdiding drawRect
This file contains 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
extension UIColor { | |
convenience init(hex: Int, alpha: CGFloat = 1.0) { | |
let red = CGFloat((hex & 0xFF0000) >> 16) / 255.0 | |
let green = CGFloat((hex & 0xFF00) >> 8) / 255.0 | |
let blue = CGFloat((hex & 0xFF)) / 255.0 | |
self.init(red:red, green:green, blue:blue, alpha:alpha) | |
} | |
} | |
@IBDesignable class AvatarView : UIImageView { | |
@IBInspectable var borderColor: UIColor = UIColor(hex:0xCCCCCC) { | |
didSet { | |
setup() | |
} | |
} | |
@IBInspectable var shadowColor: UIColor = UIColor(hex:0x888888) { | |
didSet { | |
setup() | |
} | |
} | |
func setup() { | |
self.backgroundColor = UIColor.whiteColor() | |
self.layer.cornerRadius = 12.5 | |
self.layer.shadowOpacity = 1 | |
self.layer.shadowOffset = CGSizeMake(0.05, 0.05) | |
self.layer.shadowColor = self.shadowColor.CGColor | |
self.layer.borderColor = self.borderColor.CGColor | |
self.layer.borderWidth = 1.0 | |
self.layer.masksToBounds = true | |
} | |
required init(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
setup() | |
} | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
setup() | |
} | |
override func prepareForInterfaceBuilder() { | |
setup() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment