Skip to content

Commit 6e90cdd

Browse files
committed
Clean code.
1 parent b5ea052 commit 6e90cdd

File tree

6 files changed

+77
-122
lines changed

6 files changed

+77
-122
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,13 @@ let image = UIImage.generateSettingsIcon("bell.fill", backgroundColor: .systemBl
6464
### SwiftUI
6565

6666
```swift
67-
67+
SettingsIcon("bell.fill", backgroundColor: .systemBlue)
6868
```
6969

70+
or if need generate image:
71+
72+
Image.generateSettingsIcon("bell.fill", backgroundColor: .systemBlue)
73+
7074
### AppKit
7175

7276
```swift

Sources/SPSettingsIcons/UIImageExtension.swift renamed to Sources/SPSettingsIcons/Extensions.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,18 @@ import UIKit
2525
extension UIImage {
2626

2727
public static func generateSettingsIcon(_ systemName: String, backgroundColor: UIColor) -> UIImage? {
28-
return SPSettingsIconImage.generate(systemName: systemName, backgroundColor: backgroundColor)
28+
return generate(systemName: systemName, backgroundColor: backgroundColor)
2929
}
3030
}
3131
#endif
3232

33+
#if canImport(SwiftUI)
34+
import SwiftUI
35+
36+
extension Image {
37+
38+
public static func generateSettingsIcon(_ systemName: String, backgroundColor: UIColor) -> UIImage? {
39+
return generate(systemName: systemName, backgroundColor: backgroundColor)
40+
}
41+
}
42+
#endif
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// The MIT License (MIT)
2+
// Copyright © 2022 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)
23+
import UIKit
24+
25+
var iconFontSize: CGFloat { 16 }
26+
var backgroundFontSize: CGFloat { 32 }
27+
var backgroundSystemName: String { "app.fill" }
28+
29+
func generate(systemName: String, backgroundColor: UIColor) -> UIImage? {
30+
let iconConfiguration = UIImage.SymbolConfiguration(pointSize: 32, weight: .regular)
31+
let iconImage = UIImage(systemName: systemName, withConfiguration: iconConfiguration)?.withTintColor(.white, renderingMode: .alwaysOriginal)
32+
33+
let backgroundConfiguration = UIImage.SymbolConfiguration(pointSize: 32, weight: .regular)
34+
let backgroundImage = UIImage(systemName: backgroundSystemName, withConfiguration: backgroundConfiguration)!.withTintColor(backgroundColor, renderingMode: .alwaysOriginal)
35+
36+
let size = backgroundImage.size
37+
UIGraphicsBeginImageContextWithOptions(size, false, .zero)
38+
39+
backgroundImage.draw(in: CGRect(origin: .zero, size: size))
40+
41+
if let iconImage = iconImage {
42+
let iconSize = iconImage.size
43+
iconImage.draw(in: CGRect(
44+
origin: .init(
45+
x: (size.width - iconSize.width) / 2,
46+
y: (size.height - iconSize.height) / 2
47+
),
48+
size: iconSize
49+
))
50+
}
51+
52+
let settingsImage = UIGraphicsGetImageFromCurrentImageContext()
53+
UIGraphicsEndImageContext()
54+
return settingsImage
55+
}
56+
#endif

Sources/SPSettingsIcons/ImageExtension.swift

Lines changed: 0 additions & 57 deletions
This file was deleted.

Sources/SPSettingsIcons/SPSettingsIcons.swift

Lines changed: 0 additions & 59 deletions
This file was deleted.

Sources/SPSettingsIcons/SettingsIcon.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2020
// SOFTWARE.
2121

22+
#if canImport(SwiftUI)
2223
import SwiftUI
2324

2425
public struct SettingsIcon: View {
@@ -28,13 +29,13 @@ public struct SettingsIcon: View {
2829

2930
public var body: some View {
3031
ZStack {
31-
Image(systemName: "app.fill")
32-
.font(.system(size: 32))
32+
Image(systemName: backgroundSystemName)
33+
.font(.system(size: backgroundFontSize))
3334
.foregroundColor(backgroundColor)
3435
Image(systemName: systemName)
35-
.font(.system(size: 16))
36+
.font(.system(size: iconFontSize))
3637
.foregroundColor(.white)
3738
}
3839
}
39-
4040
}
41+
#endif

0 commit comments

Comments
 (0)