Created
September 17, 2018 13:57
-
-
Save aibobrov/bfc52c085151b39517bddaa656158b29 to your computer and use it in GitHub Desktop.
IBDesignable subclass of UIView
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 | |
@IBDesignable | |
public class View: UIView { | |
// MARK: - Layer stuff | |
@IBInspectable | |
public var cornerRadius: CGFloat { | |
get { | |
return layer.cornerRadius | |
} | |
set { | |
layer.cornerRadius = newValue | |
} | |
} | |
// MARK: - IBInspectable border | |
@IBInspectable | |
public var borderColor: UIColor? { | |
get { | |
guard let color = layer.borderColor else { return nil } | |
return UIColor(cgColor: color) | |
} | |
set { | |
layer.borderColor = newValue?.cgColor | |
} | |
} | |
@IBInspectable | |
public var borderWidth: CGFloat { | |
get { | |
return layer.borderWidth | |
} | |
set { | |
layer.borderWidth = newValue | |
} | |
} | |
// MARK: - IBInspectable shadow | |
@IBInspectable | |
public var shadowRadius: CGFloat { | |
get { | |
return layer.shadowRadius | |
} | |
set { | |
layer.shadowRadius = newValue | |
} | |
} | |
@IBInspectable | |
public var shadowOpacity: Float { | |
get { | |
return layer.shadowOpacity | |
} | |
set { | |
layer.shadowOpacity = newValue | |
} | |
} | |
@IBInspectable | |
public var shadowOffset: CGSize { | |
get { | |
return layer.shadowOffset | |
} | |
set { | |
layer.shadowOffset = newValue | |
} | |
} | |
@IBInspectable | |
public var shadowColor: UIColor? { | |
get { | |
guard let color = layer.shadowColor else { return nil } | |
return UIColor(cgColor: color) | |
} | |
set { | |
layer.shadowColor = newValue?.cgColor | |
} | |
} | |
public func clear() { | |
layer.cornerRadius = 0 | |
layer.borderColor = nil | |
layer.borderWidth = 0 | |
layer.shadowRadius = 0 | |
layer.shadowOpacity = 0 | |
layer.shadowOffset = .zero | |
layer.shadowColor = nil | |
} | |
public override func prepareForInterfaceBuilder() { | |
super.prepareForInterfaceBuilder() | |
layer.cornerRadius = cornerRadius | |
layer.borderColor = borderColor?.cgColor | |
layer.borderWidth = borderWidth | |
layer.shadowRadius = shadowRadius | |
layer.shadowOpacity = shadowOpacity | |
layer.shadowOffset = shadowOffset | |
layer.shadowColor = shadowColor?.cgColor | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment