Instantly share code, notes, and snippets.
Last active
February 21, 2017 05:56
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save npu3pak/fe57cf6255b453554c06317a39ee3775 to your computer and use it in GitHub Desktop.
Кнопка со скруглениями и картинками, настраиваемая через InterfaceBuilder.
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
@IBDesignable class CustomButton: UIButton { | |
private var leftImageView: UIImageView! | |
private var rightImageView: UIImageView! | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
setUp() | |
} | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
setUp() | |
} | |
override func awakeFromNib() { | |
super.awakeFromNib() | |
setUp() | |
} | |
private func setUp() { | |
clipsToBounds = true | |
leftImageView = UIImageView(image: leftImage?.tintedImage(color ?? tintColor)) | |
rightImageView = UIImageView(image: rightImage?.tintedImage(color ?? tintColor)) | |
} | |
@IBInspectable var borderWidth: CGFloat { | |
set { layer.borderWidth = newValue } | |
get { return layer.borderWidth } | |
} | |
@IBInspectable var color: UIColor? { | |
didSet { | |
layer.borderColor = color?.cgColor | |
tintColor = color | |
} | |
} | |
@IBInspectable var cornerRadius: CGFloat { | |
set { layer.cornerRadius = newValue } | |
get { return layer.cornerRadius } | |
} | |
@IBInspectable var leftImage: UIImage? { | |
didSet { | |
leftImageView.image = leftImage?.tintedImage(color ?? tintColor) | |
} | |
} | |
@IBInspectable var rightImage: UIImage? { | |
didSet { | |
rightImageView.image = rightImage?.tintedImage(color ?? tintColor) | |
} | |
} | |
override func layoutSubviews() { | |
super.layoutSubviews() | |
addSubview(leftImageView) | |
leftImageView.translatesAutoresizingMaskIntoConstraints = false | |
let leftImageWidth = NSLayoutConstraint(item: leftImageView, | |
attribute: .width, | |
relatedBy: .equal, | |
toItem: nil, | |
attribute: .notAnAttribute, | |
multiplier: 1, | |
constant: 20) | |
let leftImageHeight = NSLayoutConstraint(item: leftImageView, | |
attribute: .height, | |
relatedBy: .equal, | |
toItem: nil, | |
attribute: .notAnAttribute, | |
multiplier: 1, | |
constant: 20) | |
leftImageView.addConstraints([leftImageWidth, leftImageHeight]) | |
let leftImageRight = NSLayoutConstraint(item: leftImageView, | |
attribute: .left, | |
relatedBy: .equal, | |
toItem: self, | |
attribute: .left, | |
multiplier: 1, | |
constant: 16) | |
let leftImageCenter = NSLayoutConstraint(item: leftImageView, | |
attribute: .centerY, | |
relatedBy: .equal, | |
toItem: self, | |
attribute: .centerY, | |
multiplier: 1, | |
constant: 0) | |
addConstraints([leftImageRight, leftImageCenter]) | |
addSubview(rightImageView) | |
rightImageView.translatesAutoresizingMaskIntoConstraints = false | |
let rightImageWidth = NSLayoutConstraint(item: rightImageView, | |
attribute: .width, | |
relatedBy: .equal, | |
toItem: nil, | |
attribute: .notAnAttribute, | |
multiplier: 1, | |
constant: 20) | |
let rightImageHeight = NSLayoutConstraint(item: rightImageView, | |
attribute: .height, | |
relatedBy: .equal, | |
toItem: nil, | |
attribute: .notAnAttribute, | |
multiplier: 1, | |
constant: 20) | |
rightImageView.addConstraints([rightImageWidth, rightImageHeight]) | |
let rightImageRight = NSLayoutConstraint(item: rightImageView, | |
attribute: .right, | |
relatedBy: .equal, | |
toItem: self, | |
attribute: .right, | |
multiplier: 1, | |
constant: -16) | |
let rightImageCenter = NSLayoutConstraint(item: rightImageView, | |
attribute: .centerY, | |
relatedBy: .equal, | |
toItem: self, | |
attribute: .centerY, | |
multiplier: 1, | |
constant: 0) | |
addConstraints([rightImageRight, rightImageCenter]) | |
} | |
} | |
extension CGColor { | |
fileprivate var uiColor: UIKit.UIColor { | |
return UIKit.UIColor(cgColor: self) | |
} | |
} | |
extension UIImage { | |
fileprivate func tintedImage(_ color: UIColor) -> UIImage { | |
let blendMode:CGBlendMode = .destinationIn | |
UIGraphicsBeginImageContextWithOptions(self.size, false, 0) | |
color.setFill() | |
let bounds = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height) | |
UIRectFill(bounds) | |
draw(in: bounds, blendMode: blendMode, alpha: 1) | |
let tintedImage = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext() | |
return tintedImage! | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment