|
| 1 | +// The MIT License (MIT) |
| 2 | +// Copyright © 2020 Ivan Vorobei ( [email protected]) |
| 3 | +// |
| 4 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +// of this software and associated documentation files (the "Software"), to deal |
| 6 | +// in the Software without restriction, including without limitation the rights |
| 7 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +// copies of the Software, and to permit persons to whom the Software is |
| 9 | +// furnished to do so, subject to the following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included in all |
| 12 | +// copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | +// SOFTWARE. |
| 21 | + |
| 22 | +#if canImport(UIKit) && (os(iOS)) |
| 23 | +import UIKit |
| 24 | + |
| 25 | +/** |
| 26 | + SparrowKit: Small action button. |
| 27 | + Usually using at bottom of screen. |
| 28 | + */ |
| 29 | +open class SPSmallActionButton: SPDimmedButton { |
| 30 | + |
| 31 | + // MARK: - Data |
| 32 | + |
| 33 | + /** |
| 34 | + SparrowKit: Higlight style when button pressing. |
| 35 | + |
| 36 | + If set content, only label and image change opacity. |
| 37 | + If choosed background, area of button will change opacity. |
| 38 | + */ |
| 39 | + open var higlightStyle = HiglightStyle.default |
| 40 | + |
| 41 | + // MARK: - Init |
| 42 | + |
| 43 | + open override func commonInit() { |
| 44 | + super.commonInit() |
| 45 | + titleLabel?.font = UIFont.preferredFont(forTextStyle: .subheadline, weight: .bold, addPoints: -1) |
| 46 | + titleLabel?.numberOfLines = 1 |
| 47 | + titleImageInset = 6 |
| 48 | + contentEdgeInsets = .init(horizontal: 10, vertical: .zero) |
| 49 | + } |
| 50 | + |
| 51 | + // MARK: - Public |
| 52 | + |
| 53 | + /** |
| 54 | + SparrowKit: Wrapper of set content and color of button. |
| 55 | + |
| 56 | + - parameter title: Text which using like title. |
| 57 | + - parameter icon: Object of `UIImage`, using like icon. |
| 58 | + - parameter colorise: Color of button in default state. |
| 59 | + */ |
| 60 | + public func set(title: String, icon: UIImage?, colorise: SPDimmedButton.Colorise) { |
| 61 | + setTitle(title) |
| 62 | + if let icon = icon { |
| 63 | + setImage(icon.alwaysTemplate) |
| 64 | + } |
| 65 | + applyDefaultAppearance(with: colorise) |
| 66 | + } |
| 67 | + |
| 68 | + // MARK: - Layout |
| 69 | + |
| 70 | + open override func layoutSubviews() { |
| 71 | + super.layoutSubviews() |
| 72 | + layer.cornerRadius = frame.height / 2 |
| 73 | + } |
| 74 | + |
| 75 | + open override func sizeThatFits(_ size: CGSize) -> CGSize { |
| 76 | + let superSize = super.sizeThatFits(size) |
| 77 | + var width = superSize.width |
| 78 | + if width < 70 { width = 70 } |
| 79 | + |
| 80 | + var height = superSize.height + 12 |
| 81 | + if let titleLabel = titleLabel, let imageView = imageView, let _ = imageView.image { |
| 82 | + if titleLabel.frame.height > 0 && imageView.frame.height > 0 { |
| 83 | + let imageCorrection = imageView.frame.height - titleLabel.frame.height |
| 84 | + height -= imageCorrection |
| 85 | + } |
| 86 | + } |
| 87 | + return CGSize(width: width, height: height) |
| 88 | + } |
| 89 | + |
| 90 | + // MARK: - Ovveride |
| 91 | + |
| 92 | + open override var isHighlighted: Bool { |
| 93 | + didSet { |
| 94 | + switch higlightStyle { |
| 95 | + case .content: |
| 96 | + for view in [imageView, titleLabel] { view?.alpha = isHighlighted ? highlightOpacity : 1 } |
| 97 | + case .background: |
| 98 | + let color = backgroundColor |
| 99 | + backgroundColor = color?.withAlphaComponent(isHighlighted ? highlightOpacity : 1) |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + open override func setTitle(_ title: String?, for state: UIControl.State) { |
| 105 | + super.setTitle(title?.uppercased(), for: state) |
| 106 | + } |
| 107 | + |
| 108 | + // MARK: - Models |
| 109 | + |
| 110 | + public enum HiglightStyle { |
| 111 | + |
| 112 | + case content |
| 113 | + case background |
| 114 | + |
| 115 | + static var `default`: HiglightStyle { |
| 116 | + return .background |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | +#endif |
0 commit comments