Skip to content

Commit 51cd546

Browse files
committed
SVGView
1 parent a3553a4 commit 51cd546

File tree

6 files changed

+148
-3
lines changed

6 files changed

+148
-3
lines changed

Examples/Basic.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
D961846D220FDD1200C59D9B /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = D961846C220FDD1200C59D9B /* Assets.xcassets */; };
1313
D9618470220FDD1200C59D9B /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = D961846E220FDD1200C59D9B /* LaunchScreen.storyboard */; };
1414
D9742E1E27D4877300E02FFD /* SwiftDraw in Frameworks */ = {isa = PBXBuildFile; productRef = D9742E1D27D4877300E02FFD /* SwiftDraw */; };
15+
D9AC57BF2D65E86A005ACBFF /* GalleryView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9AC57BE2D65E86A005ACBFF /* GalleryView.swift */; };
1516
D9EE86AF2A4EC94E00C7CAE1 /* Samples.bundle in Resources */ = {isa = PBXBuildFile; fileRef = D94D5BE22A4EC906001DCD83 /* Samples.bundle */; };
1617
/* End PBXBuildFile section */
1718

@@ -37,6 +38,7 @@
3738
D961846F220FDD1200C59D9B /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
3839
D9618471220FDD1200C59D9B /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
3940
D9742E1C27D4875100E02FFD /* SwiftDraw */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = SwiftDraw; path = ..; sourceTree = "<group>"; };
41+
D9AC57BE2D65E86A005ACBFF /* GalleryView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GalleryView.swift; sourceTree = "<group>"; };
4042
D9ACD7A0220FDE04009717CF /* SwiftDraw.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = SwiftDraw.framework; sourceTree = BUILT_PRODUCTS_DIR; };
4143
/* End PBXFileReference section */
4244

@@ -75,6 +77,7 @@
7577
D9618464220FDD1100C59D9B /* Sources */ = {
7678
isa = PBXGroup;
7779
children = (
80+
D9AC57BE2D65E86A005ACBFF /* GalleryView.swift */,
7881
D9618465220FDD1100C59D9B /* AppDelegate.swift */,
7982
D9618467220FDD1100C59D9B /* ViewController.swift */,
8083
D961846C220FDD1200C59D9B /* Assets.xcassets */,
@@ -169,6 +172,7 @@
169172
files = (
170173
D9618468220FDD1100C59D9B /* ViewController.swift in Sources */,
171174
D9618466220FDD1100C59D9B /* AppDelegate.swift in Sources */,
175+
D9AC57BF2D65E86A005ACBFF /* GalleryView.swift in Sources */,
172176
);
173177
runOnlyForDeploymentPostprocessing = 0;
174178
};

Examples/Sources/AppDelegate.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
//
3131

3232
import UIKit
33+
import SwiftUI
3334

3435
@UIApplicationMain
3536
class AppDelegate: UIResponder, UIApplicationDelegate {
@@ -40,7 +41,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
4041
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
4142

4243
let window = UIWindow()
43-
window.rootViewController = UINavigationController(rootViewController: ViewController())
44+
window.rootViewController = UINavigationController(rootViewController: UIHostingController(rootView: GalleryView()))
4445
window.makeKeyAndVisible()
4546
self.window = window
4647

Examples/Sources/GalleryView.swift

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//
2+
// GalleryView.swift
3+
// SwiftDraw
4+
//
5+
// Created by Simon Whitty on 19/2/25.
6+
// Copyright 2019 Simon Whitty
7+
//
8+
// Distributed under the permissive zlib license
9+
// Get the latest version from here:
10+
//
11+
// https://github.com/swhitty/SwiftDraw
12+
//
13+
// This software is provided 'as-is', without any express or implied
14+
// warranty. In no event will the authors be held liable for any damages
15+
// arising from the use of this software.
16+
//
17+
// Permission is granted to anyone to use this software for any purpose,
18+
// including commercial applications, and to alter it and redistribute it
19+
// freely, subject to the following restrictions:
20+
//
21+
// 1. The origin of this software must not be misrepresented; you must not
22+
// claim that you wrote the original software. If you use this software
23+
// in a product, an acknowledgment in the product documentation would be
24+
// appreciated but is not required.
25+
//
26+
// 2. Altered source versions must be plainly marked as such, and must not be
27+
// misrepresented as being the original software.
28+
//
29+
// 3. This notice may not be removed or altered from any source distribution.
30+
//
31+
32+
import SwiftDraw
33+
import SwiftUI
34+
35+
struct GalleryView: View {
36+
37+
var imageNames: [String] = [
38+
"avocado.svg",
39+
"angry.svg",
40+
"dish.svg",
41+
"mouth-open.svg",
42+
"sleepy.svg",
43+
"smile.svg",
44+
"snake.svg",
45+
"spider.svg",
46+
"star-struck.svg",
47+
"worried.svg",
48+
"yawning.svg",
49+
"thats-no-moon.svg",
50+
"alert.svg"
51+
]
52+
53+
var body: some View {
54+
if #available(iOS 15.0, *) {
55+
ScrollView {
56+
LazyVStack(spacing: 20) {
57+
ForEach(imageNames, id: \.self) { name in
58+
SVGView(name, bundle: .samples)
59+
.aspectRatio(contentMode: .fit)
60+
.padding([.leading, .trailing], 10)
61+
// .frame(maxWidth: 320)
62+
}
63+
}
64+
.background(Color.white)
65+
}
66+
67+
}
68+
}
69+
}
File renamed without changes.

SwiftDraw/Image.swift renamed to SwiftDraw/SVG.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ public final class SVG: NSObject {
7373
}
7474
}
7575

76-
@available(*, deprecated, renamed: "SVG")
77-
public typealias Image = SVG
76+
@available(*, unavailable, renamed: "SVG")
77+
public enum Image { }
7878

7979
#else
8080

SwiftDraw/SVGView.swift

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//
2+
// SVGView.swift
3+
// SwiftDraw
4+
//
5+
// Created by Simon Whitty on 19/2/25.
6+
// Copyright 2019 Simon Whitty
7+
//
8+
// Distributed under the permissive zlib license
9+
// Get the latest version from here:
10+
//
11+
// https://github.com/swhitty/SwiftDraw
12+
//
13+
// This software is provided 'as-is', without any express or implied
14+
// warranty. In no event will the authors be held liable for any damages
15+
// arising from the use of this software.
16+
//
17+
// Permission is granted to anyone to use this software for any purpose,
18+
// including commercial applications, and to alter it and redistribute it
19+
// freely, subject to the following restrictions:
20+
//
21+
// 1. The origin of this software must not be misrepresented; you must not
22+
// claim that you wrote the original software. If you use this software
23+
// in a product, an acknowledgment in the product documentation would be
24+
// appreciated but is not required.
25+
//
26+
// 2. Altered source versions must be plainly marked as such, and must not be
27+
// misrepresented as being the original software.
28+
//
29+
// 3. This notice may not be removed or altered from any source distribution.
30+
//
31+
32+
#if canImport(SwiftUI)
33+
import SwiftUI
34+
35+
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
36+
public struct SVGView: View {
37+
38+
public init(_ name: String, bundle: Bundle = .main) {
39+
self.svg = SVG(named: name, in: bundle)
40+
}
41+
42+
public init(svg: SVG) {
43+
self.svg = svg
44+
}
45+
46+
private let svg: SVG?
47+
48+
public var body: some View {
49+
if let svg {
50+
Canvas(
51+
opaque: false,
52+
colorMode: .linear,
53+
rendersAsynchronously: false
54+
) { ctx, size in
55+
ctx.draw(svg, in: CGRect(origin: .zero, size: size))
56+
}
57+
.frame(idealWidth: svg.size.width, idealHeight: svg.size.height)
58+
}
59+
}
60+
}
61+
62+
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
63+
public extension GraphicsContext {
64+
65+
func draw(_ image: SVG, in rect: CGRect? = nil) {
66+
withCGContext {
67+
$0.draw(image, in: rect)
68+
}
69+
}
70+
}
71+
#endif

0 commit comments

Comments
 (0)