SwiftUIWithSVG - User sag file with swift

Overview

更新

可以直接使用svg檔案, 調整下面設定可以直接用foregroundColor調整顏色.

settingSvgImage.png

專案下載

https://github.com/NullRa/SwiftUIWithSVG/blob/main/README.md

直接用比較簡單 以下都僅供參考

直接暴力使用SVG檔案

Image(uiImage: UIImage(named: "tab_home")!)

加入一個備用檔案避免optional crash

包裝成一個svgToImg function

private func svgToImg(svgNamed:String, preImgNamed:String) -> Image {
        guard let uiImg = UIImage(named: svgNamed) else {
            return Image(systemName: preImgNamed)
        }
        return Image(uiImage: uiImg)
    }

使用svgToImg function

self.svgToImg(svgNamed: "tab_home2", preImgNamed: "house.fill")
            .padding()
            .background(Color.red)
            .cornerRadius(30)

更進階的用法

調整svg檔案的圖片大小

private func svgToImg(svgNamed:String, preImgNamed:String) -> Image {
        guard let uiImg = UIImage(named: svgNamed) else {
            return Image(systemName: preImgNamed)
        }
        let width: CGFloat = 28
        let size = CGSize(width: width, height: uiImg.size.height * width / uiImg.size.width)
        let renderer = UIGraphicsImageRenderer(size: size)
        let newImg = renderer.image { (context) in
            uiImg.draw(in: renderer.format.bounds)
        }
        return Image(uiImage: newImg)
    }

未解決的問題

無法直接調整svg檔案顏色,查過有個套件svgKit可以使用,有這方面需求可以往這方向去找答案.

You might also like...
Fast Swift Views layouting without auto layout. No magic, pure code, full control and blazing fast. Concise syntax, intuitive, readable & chainable. [iOS/macOS/tvOS/CALayer]
Fast Swift Views layouting without auto layout. No magic, pure code, full control and blazing fast. Concise syntax, intuitive, readable & chainable. [iOS/macOS/tvOS/CALayer]

Extremely Fast views layouting without auto layout. No magic, pure code, full control and blazing fast. Concise syntax, intuitive, readable & chainabl

Simple static table views for iOS in Swift.
Simple static table views for iOS in Swift.

Simple static table views for iOS in Swift. Static's goal is to separate model data from presentation. Rows and Sections are your “view models” for yo

A declarative Auto Layout DSL for Swift :iphone::triangular_ruler:
A declarative Auto Layout DSL for Swift :iphone::triangular_ruler:

Cartography 📱 📐 Using Cartography, you can set up your Auto Layout constraints in declarative code and without any stringly typing! In short, it all

A Swift port of the Cassowary linear constraint solver

Cassowary Swift A Swift port of the Cassowary linear constraints solver. Tested on OS X, iOS and Linux. Example usage let solver = Solver() let left

An easy way to create and layout UI components for iOS (Swift version).
An easy way to create and layout UI components for iOS (Swift version).

Introduction Cupcake is a framework that allow you to easily create and layout UI components for iOS 8.0+. It use chaining syntax and provides some fr

Lightweight Swift framework for Apple's Auto-Layout
Lightweight Swift framework for Apple's Auto-Layout

I am glad to share with you a lightweight Swift framework for Apple's Auto-Layout. It helps you write readable and compact UI code using simple API. A

An Impressive Auto Layout DSL for  iOS, tvOS & OSX. & It is written in pure swift.
An Impressive Auto Layout DSL for iOS, tvOS & OSX. & It is written in pure swift.

KVConstraintKit KVConstraintKit is a DSL to make easy & impressive Auto Layout constraints on iOS, tvOS & OSX with Swift Installation Using CocoaPods

A compact but full-featured Auto Layout DSL for Swift
A compact but full-featured Auto Layout DSL for Swift

Mortar allows you to create Auto Layout constraints using concise, simple code statements. Use this: view1.m_right |=| view2.m_left - 12.0 Instead of:

The ultimate API for iOS & OS X Auto Layout — impressively simple, immensely powerful. Objective-C and Swift compatible.
The ultimate API for iOS & OS X Auto Layout — impressively simple, immensely powerful. Objective-C and Swift compatible.

The ultimate API for iOS & OS X Auto Layout — impressively simple, immensely powerful. PureLayout extends UIView/NSView, NSArray, and NSLayoutConstrai

Owner
Ting, Shao-Chiech
Ting, Shao-Chiech
Custom UIView class that hosts an array of UIbuttons that have an 'underline' UIView beneath them which moves from button to button when the user presses on them.

Swift-Underlined-Button-Bar Custom UIView class that hosts an array of UIbuttons that have an 'underline' UIView beneath them which moves from button

Justin Cook 3 Aug 4, 2022
A multi-user Mastodon client for iOS.

Tootbot A multi-user Mastodon client for iOS. Getting Started Tootbot must be built with Xcode 8.3 or later. The xcode-install gem can help with multi

Tootbot 27 Aug 9, 2022
MisterFusion is Swift DSL for AutoLayout. It is the extremely clear, but concise syntax, in addition, can be used in both Swift and Objective-C. Support Safe Area and Size Class.

MisterFusion MisterFusion makes more easier to use AutoLayout in Swift & Objective-C code. Features Simple And Concise Syntax Use in Swift and Objecti

Taiki Suzuki 316 Nov 17, 2022
VidyoPlatform Basic CustomLayouts Reference App for iOS (Swift)VidyoPlatform Basic CustomLayouts Reference App for iOS (Swift)

VidyoPlatform Basic CustomLayouts Reference App for iOS (Swift) VidyoPlatform reference application highlighting how to integrate video chat into a na

Taras Melko 0 Nov 19, 2021
100-days-swift-project-8 - The eighth project from 100 days of Swift course

7 Swifty Words This is the eighth project from Hacking With Swift 100 days of Sw

Bruno Guirra 0 Jan 24, 2022
SwiftLanguageWeather-master - Swift Language Weather is an iOS weather app developed in Swift 4

Swift Language Weather SwiftWeather has renamed to Swift Language Weather. Becau

Kushal Shingote 1 Feb 5, 2022
Fancy Swift implementation of the Visual Format Language (experimental and doesn't work with the recent version of Swift)

VFLToolbox Autolayout is awesome! VFL a.k.a Visual Format Language is even more awesome because it allows you to shorten constraints setting code. The

0xc010d 144 Jun 29, 2022
BrickKit is a delightful layout library for iOS and tvOS. It is written entirely in Swift!

BrickKit is a delightful layout library for iOS and tvOS. It is written entirely in Swift! Deprecated BrickKit is being phased out at Wayfair, and the

Wayfair Tech – Archive 608 Sep 15, 2022
FlexLayout adds a nice Swift interface to the highly optimized facebook/yoga flexbox implementation. Concise, intuitive & chainable syntax.

FlexLayout adds a nice Swift interface to the highly optimized Yoga flexbox implementation. Concise, intuitive & chainable syntax. Flexbox is an incre

layoutBox 1.7k Dec 30, 2022
A powerful Swift programmatic UI layout framework.

Build dynamic and beautiful user interfaces like a boss, with Swift. Neon is built around how user interfaces are naturally and intuitively designed.

Mike 4.6k Dec 26, 2022