Skip to content

Commit 404c744

Browse files
committed
Added SPLargeActionButton.
1 parent 9362764 commit 404c744

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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: Large action button.
27+
Usually using at bottom of screen.
28+
*/
29+
open class SPLargeActionButton: 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: .headline, addPoints: 1)
46+
titleLabel?.numberOfLines = 1
47+
titleImageInset = 6
48+
}
49+
50+
// MARK: - Public
51+
52+
/**
53+
SparrowKit: Wrapper of set content and color of button.
54+
55+
- parameter title: Text which using like title.
56+
- parameter icon: Object of `UIImage`, using like icon.
57+
- parameter colorise: Color of button in default state.
58+
*/
59+
public func set(title: String, icon: UIImage?, colorise: SPDimmedButton.Colorise) {
60+
setTitle(title)
61+
if let icon = icon {
62+
setImage(icon.alwaysTemplate)
63+
}
64+
applyDefaultAppearance(with: colorise)
65+
}
66+
67+
// MARK: - Layout
68+
69+
open override func layoutSubviews() {
70+
super.layoutSubviews()
71+
roundCorners(radius: 15)
72+
}
73+
74+
open override func sizeThatFits(_ size: CGSize) -> CGSize {
75+
return CGSize(width: size.width, height: 50)
76+
}
77+
78+
// MARK: - Ovveride
79+
80+
open override var isHighlighted: Bool {
81+
didSet {
82+
let higlightAlpha: CGFloat = 0.6
83+
switch higlightStyle {
84+
case .content:
85+
for view in [imageView, titleLabel] { view?.alpha = isHighlighted ? higlightAlpha : 1 }
86+
case .background:
87+
let color = backgroundColor
88+
backgroundColor = color?.withAlphaComponent(isHighlighted ? higlightAlpha : 1)
89+
}
90+
}
91+
}
92+
93+
// MARK: - Models
94+
95+
public enum HiglightStyle {
96+
97+
case content
98+
case background
99+
100+
static var `default`: HiglightStyle {
101+
return .background
102+
}
103+
}
104+
}
105+
#endif

0 commit comments

Comments
 (0)