Skip to content
This repository was archived by the owner on Sep 19, 2024. It is now read-only.

SwiftUI resource support for Image, Color, Font #37

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Library/SwiftUI/SwiftUI.Color+ColorResource.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// SwiftUI.Color+ColorResource.swift
// R.swift Library
//
// Created by Tobeas Brennan on 30-11-19.
// From: https://github.com/mac-cain13/R.swift.Library
// License: MIT License
//

import SwiftUI

@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public extension Color {
/**
Returns the SwiftUI.Color from this resource (R.color.*)

- parameter resource: The resource you want the image of (R.color.*)

- returns: A SwiftUI.Color for the given resource (R.color.*)
*/
init(_ resource: ColorResourceType) {
self.init(resource.name, bundle: resource.bundle)
}
}

@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public extension ColorResource {
var color: Color {
SwiftUI.Color(self)
}
}
32 changes: 32 additions & 0 deletions Library/SwiftUI/SwiftUI.Font+FontResource.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// SwiftUI.Font+FontResource.swift
// R.swift Library
//
// Created by Tobeas Brennan on 30-11-19.
// From: https://github.com/mac-cain13/R.swift.Library
// License: MIT License
//

import SwiftUI

@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public extension SwiftUI.Font {
/**
Creates and returns a SwiftUI.Font for the specified font resource (R.font.*) and size.

- parameter resource: The font resource (R.font.*) for the specific font to load
- parameter size: The size (in points) to which the font is scaled. This value must be greater than 0.0.

- returns: A SwiftUI.Font object of the specified font resource and size.
*/
init(_ resource: FontResourceType, size: CGFloat) {
self = .custom(resource.fontName, size: size)
}
}

@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public extension FontResource {
func font(size: CGFloat) -> SwiftUI.Font {
SwiftUI.Font(self, size: size)
}
}
31 changes: 31 additions & 0 deletions Library/SwiftUI/SwiftUI.Image+ImageResource.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// SwiftUI.Image+ImageResource.swift
// R.swift Library
//
// Created by Tobeas Brennan on 30-11-19.
// From: https://github.com/mac-cain13/R.swift.Library
// License: MIT License
//

import SwiftUI

@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public extension SwiftUI.Image {
/**
Returns the image from this resource (R.image.*)

- parameter resource: The resource you want the image of (R.image.*)

- returns: A SwiftUI.Image for the given resource (R.image.*)
*/
init(_ resource: ImageResourceType) {
self.init(resource.name, bundle: resource.bundle)
}
}

@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public extension ImageResource {
var image: SwiftUI.Image {
SwiftUI.Image(self)
}
}
10 changes: 5 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// swift-tools-version:5.0
// swift-tools-version:5.2

import PackageDescription

let package = Package(
name: "R.swift.Library",
platforms: [
.iOS(.v9),
.tvOS(.v9),
.watchOS(.v2),
.iOS(.v11),
.tvOS(.v9),
.watchOS(.v2)
],
products: [
.library(name: "Rswift", targets: ["Rswift"]),
.library(name: "RswiftDynamic", type: .dynamic, targets: ["Rswift"])
],
targets: [
.target(name: "Rswift", path: "Library")
.target(name: "Rswift", path: "Library")
]
)