-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
140 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
59
Sources/SwiftHelperUIKitExtensions/Extensions/UIColor.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
|
File renamed without changes.