-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from surfstudio/CommonButton
Common button и UIImageExtensions
- Loading branch information
Showing
5 changed files
with
241 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// | ||
// CommonButton.swift | ||
// Utils | ||
// | ||
// Created by Александр Чаусов on 28/01/2019. | ||
// Copyright © 2019 Surf. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
open class CommonButton: UIButton { | ||
|
||
// MARK: - Public Properties | ||
|
||
public var borderColor: UIColor? { | ||
didSet { | ||
layer.borderColor = borderColor?.cgColor | ||
} | ||
} | ||
|
||
public var borderWidth: CGFloat = 0 { | ||
didSet { | ||
layer.borderWidth = borderWidth | ||
} | ||
} | ||
|
||
public var cornerRadius: CGFloat { | ||
get { | ||
return layer.cornerRadius | ||
} | ||
set { | ||
layer.cornerRadius = newValue | ||
layer.masksToBounds = newValue > 0 | ||
} | ||
} | ||
|
||
/// Increase touch area | ||
public var addedTouchArea: CGFloat = 0.0 | ||
|
||
// MARK: - Initialization | ||
|
||
override public init(frame: CGRect) { | ||
super.init(frame: frame) | ||
} | ||
|
||
public required init?(coder aDecoder: NSCoder) { | ||
super.init(coder: aDecoder) | ||
} | ||
|
||
override open func awakeFromNib() { | ||
super.awakeFromNib() | ||
} | ||
|
||
// MARK: - UIButton | ||
|
||
override open func point(inside point: CGPoint, with event: UIEvent?) -> Bool { | ||
let newBound = CGRect( | ||
x: bounds.origin.x - addedTouchArea, | ||
y: bounds.origin.y - addedTouchArea, | ||
width: bounds.width + 2 * addedTouchArea, | ||
height: bounds.height + 2 * addedTouchArea | ||
) | ||
return newBound.contains(point) | ||
} | ||
|
||
// MARK: - Public Methods | ||
|
||
/// Method set title for all states | ||
public func setTitleForAllState(_ title: String?) { | ||
setTitle(title, for: .normal) | ||
setTitle(title, for: .disabled) | ||
setTitle(title, for: .highlighted) | ||
setTitle(title, for: .selected) | ||
} | ||
|
||
/// Method set image for all states | ||
/// If use alpha image with alpha mask will set for disabled, highlighted, selected states | ||
/// - Parameters: | ||
/// - image: Optional value for set button image | ||
/// - alpha: Optional value for disabled, highlighted, selected states | ||
public func setImageForAllState(_ image: UIImage?, alpha: CGFloat? = nil) { | ||
let highlightedImage = alpha != nil | ||
? image?.mask(with: alpha ?? 0) | ||
: image | ||
setImage(image, for: .normal) | ||
setImage(highlightedImage, for: .disabled) | ||
setImage(highlightedImage, for: .highlighted) | ||
setImage(highlightedImage, for: .selected) | ||
} | ||
|
||
/// Method will set background color for control state | ||
public func set(backgroundColor: UIColor, for state: UIControl.State) { | ||
setBackgroundImage(UIImage(color: backgroundColor), for: state) | ||
} | ||
|
||
/// Method will set background color for all choosed control states | ||
public func set(backgroundColor: UIColor, for states: [UIControl.State]) { | ||
states.forEach { setBackgroundImage(UIImage(color: backgroundColor), for:$0) } | ||
} | ||
|
||
/// Method will set title color for all choosed control states | ||
public func set(titleColor: UIColor, for states: [UIControl.State]) { | ||
states.forEach { setTitleColor(titleColor, for: $0) } | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// | ||
// UIImageExtensions.swift | ||
// Utils | ||
// | ||
// Created by Vladislav Krupenko on 03/11/2019. | ||
// Copyright © 2019 Surf. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
public extension UIImage { | ||
|
||
/// Init method for creating UIImage of a given color | ||
/// - Parameters: | ||
/// - color: Optional value, by default clear color | ||
/// - size: Optional value, by default size 1*1 | ||
convenience init?(color: UIColor?, size: CGSize = CGSize(width: 1, height: 1)) { | ||
let color = color ?? UIColor.clear | ||
let rect = CGRect(origin: .zero, size: size) | ||
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0) | ||
color.setFill() | ||
UIRectFill(rect) | ||
let image = UIGraphicsGetImageFromCurrentImageContext() | ||
UIGraphicsEndImageContext() | ||
|
||
guard let cgImage = image?.cgImage else { | ||
return nil | ||
} | ||
self.init(cgImage: cgImage) | ||
} | ||
|
||
/// Method returns UIImage with given tint color | ||
func mask(with color: UIColor) -> UIImage { | ||
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale) | ||
defer { UIGraphicsEndImageContext() } | ||
|
||
guard let context = UIGraphicsGetCurrentContext() else { | ||
return self | ||
} | ||
context.translateBy(x: 0, y: self.size.height) | ||
context.scaleBy(x: 1.0, y: -1.0) | ||
context.setBlendMode(.normal) | ||
|
||
let rect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height) | ||
guard let mask = self.cgImage else { | ||
return self | ||
} | ||
context.clip(to: rect, mask: mask) | ||
|
||
color.setFill() | ||
context.fill(rect) | ||
|
||
guard let newImage = UIGraphicsGetImageFromCurrentImageContext() else { | ||
return self | ||
} | ||
return newImage | ||
} | ||
|
||
/// Method return UIImage with given alpha | ||
func mask(with alpha: CGFloat) -> UIImage { | ||
UIGraphicsBeginImageContextWithOptions(size, false, scale) | ||
draw(at: .zero, blendMode: .normal, alpha: alpha) | ||
let newImage = UIGraphicsGetImageFromCurrentImageContext() | ||
UIGraphicsEndImageContext() | ||
return newImage ?? self | ||
} | ||
|
||
} |