Skip to content

Commit

Permalink
initiial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Bollerd committed Jan 21, 2024
1 parent 82317fb commit c620e75
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 6 deletions.
15 changes: 9 additions & 6 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@
import PackageDescription

let package = Package(
name: "SwiftHelperUIKit",
name: "SwiftHelperUIKitExtensions",
platforms: [
.iOS("16.0")
],
products: [
// Products define the executables and libraries a package produces, making them visible to other packages.
.library(
name: "SwiftHelperUIKit",
targets: ["SwiftHelperUIKit"]),
name: "SwiftHelperUIKitExtensions",
targets: ["SwiftHelperUIKitExtensions"]),
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.target(
name: "SwiftHelperUIKit"),
name: "SwiftHelperUIKitExtensions"),
.testTarget(
name: "SwiftHelperUIKitTests",
dependencies: ["SwiftHelperUIKit"]),
name: "SwiftHelperUIKitExtensionsTests",
dependencies: ["SwiftHelperUIKitExtensions"]),
]
)
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# SwiftHelperUIKitExtensions
Extensions for Swift Classes using UIKit Framework. Move out from SwiftHelperExtensions to enable a code check for the pure SwiftUI based part.

The pure SwiftUI based functions and extensions are available at this [Github link](https://github.com/Bollerd/SwiftHelper)

## Swift Standard Libraries

Extensions for the different Objects are located in the Extensions folder. One file for each standard file is locate in this folder.
59 changes: 59 additions & 0 deletions Sources/SwiftHelperUIKitExtensions/Extensions/UIColor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import UIKit

public extension UIColor {
/// get back the color values for red, green, blue and alpha channel
var rgba: (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) {
var red: CGFloat = 0
var green: CGFloat = 0
var blue: CGFloat = 0
var alpha: CGFloat = 0
getRed(&red, green: &green, blue: &blue, alpha: &alpha)

return (red, green, blue, alpha)
}

/// get the xy representation of the rgb color required for deconz rest api (zigbee/hue)
var xyColor: (x:CGFloat, y: CGFloat) {
let r = self.rgba.red
let g = self.rgba.green
let b = self.rgba.blue
let a = self.rgba.alpha
// convert rgb to required xy color format
let red = (r > 0.04045) ? pow((r + 0.055) / (1.0 + 0.055), 2.4) : (r / 12.92)
let green = (g > 0.04045) ? pow((g + 0.055) / (1.0 + 0.055), 2.4) : (g / 12.92)
let blue = (b > 0.04045) ? pow((b + 0.055) / (1.0 + 0.055), 2.4) : (b / 12.92)
let X = red * 0.649926 + green * 0.103455 + blue * 0.197109
let Y = red * 0.234327 + green * 0.743075 + blue * 0.022598
let Z = red * 0.0000000 + green * 0.053077 + blue * 1.035763
let x = X / (X + Y + Z)
let y = Y / (X + Y + Z)

return (x, y)
}

/// initialize new color from HTML Color Code
static func hexStringToUIColor (hex:String) -> UIColor {
var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()

if (cString.hasPrefix("#")) {
cString.remove(at: cString.startIndex)
}

if ((cString.count) != 6) {
return UIColor.gray
}

var rgbValue:UInt64 = 0
Scanner(string: cString).scanHexInt64(&rgbValue)

let newColor = UIColor(
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: CGFloat(1.0)
)

return newColor
}
}

64 changes: 64 additions & 0 deletions Sources/SwiftHelperUIKitExtensions/Extensions/View.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import SwiftUI
import UIKit

public extension View {
func snapshot<T: ObservableObject>(environmentObject: T,backgroundColor: Color?, namedColor: String?) -> UIImage {
let controller = UIHostingController(rootView: self.environmentObject(environmentObject))
let view = controller.view

let targetSize = controller.view.intrinsicContentSize
view?.bounds = CGRect(origin: .zero, size: targetSize)
if let colorName = namedColor {
view?.backgroundColor = UIColor(named: colorName)

}

if let bgColor = backgroundColor {
view?.backgroundColor = UIColor(bgColor)
}

let renderer = UIGraphicsImageRenderer(size: targetSize)

return renderer.image { _ in
view?.drawHierarchy(in: controller.view.bounds, afterScreenUpdates: true)
}
}

func snapshot(backgroundColor: Color?, namedColor: String?) -> UIImage {
let controller = UIHostingController(rootView: self)
let view = controller.view

let targetSize = controller.view.intrinsicContentSize
view?.bounds = CGRect(origin: .zero, size: targetSize)
if let colorName = namedColor {
view?.backgroundColor = UIColor(named: colorName)

}

if let bgColor = backgroundColor {
view?.backgroundColor = UIColor(bgColor)
}

let renderer = UIGraphicsImageRenderer(size: targetSize)

return renderer.image { _ in
view?.drawHierarchy(in: controller.view.bounds, afterScreenUpdates: true)
}
}

/// Sets the text color for a navigation bar title.
/// - Parameter color: Color the title should be
///
/// Supports both regular and large titles.
@available(iOS 14, *)
func navigationBarTitleTextColor(_ color: Color) -> some View {
let uiColor = UIColor(color)

// Set appearance for both normal and large sizes.
UINavigationBar.appearance().titleTextAttributes = [.foregroundColor: uiColor ]
UINavigationBar.appearance().largeTitleTextAttributes = [.foregroundColor: uiColor ]

return self
}
}

0 comments on commit c620e75

Please sign in to comment.