From 5dd09b667c93e8fc567312fbc10ec3862ebaf963 Mon Sep 17 00:00:00 2001 From: Rakuyo Date: Tue, 7 May 2024 10:26:15 +0800 Subject: [PATCH 1/2] feat: Put the CGFloat extension under the namespace --- Sources/Core/Extensions/CGFloat+RAK.swift | 14 +++++++------- Sources/Core/Extensions/Extendable.swift | 1 + 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Sources/Core/Extensions/CGFloat+RAK.swift b/Sources/Core/Extensions/CGFloat+RAK.swift index 41ab633..640257c 100644 --- a/Sources/Core/Extensions/CGFloat+RAK.swift +++ b/Sources/Core/Extensions/CGFloat+RAK.swift @@ -8,19 +8,19 @@ import UIKit -extension CGFloat { +extension Extendable where Base == CGFloat { /// Get 0.5pt value - public static var halfPoint: Self { CGFloat(1).scale } - - /// `self / scale` - public var scale: Self { - let _scale: Self = { + public static var halfPoint: Base { Base(1).rak.scale } + + /// `float / scale` + public var scale: Base { + let _scale: Base = { #if os(watchOS) return 1 #else return UIScreen.rak.scale #endif }() - return self / _scale + return base / _scale } } diff --git a/Sources/Core/Extensions/Extendable.swift b/Sources/Core/Extensions/Extendable.swift index f2dd7f0..f9efcfd 100644 --- a/Sources/Core/Extensions/Extendable.swift +++ b/Sources/Core/Extensions/Extendable.swift @@ -68,6 +68,7 @@ import class Foundation.NSObject; extension NSObject : NamespaceP import struct Foundation.Date; extension Date : NamespaceProviding { } import struct Foundation.URL; extension URL : NamespaceProviding { } import struct Foundation.Data; extension Data : NamespaceProviding { } +import struct UIKit.CGFloat; extension CGFloat : NamespaceProviding { } import struct UIKit.CGPoint; extension CGPoint : NamespaceProviding { } import struct UIKit.CGSize; extension CGSize : NamespaceProviding { } import struct UIKit.CGRect; extension CGRect : NamespaceProviding { } From 4d06e989245da2659fad2b14edece1d6032b87db Mon Sep 17 00:00:00 2001 From: Rakuyo Date: Tue, 7 May 2024 10:27:14 +0800 Subject: [PATCH 2/2] feat: Add `alignPixel` func to CGFloat extension --- Sources/Core/Extensions/CGFloat+RAK.swift | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Sources/Core/Extensions/CGFloat+RAK.swift b/Sources/Core/Extensions/CGFloat+RAK.swift index 640257c..f883b27 100644 --- a/Sources/Core/Extensions/CGFloat+RAK.swift +++ b/Sources/Core/Extensions/CGFloat+RAK.swift @@ -23,4 +23,12 @@ extension Extendable where Base == CGFloat { }() return base / _scale } + + /// Align floating point values to the pixels of the current device + /// + /// - Parameter rule: A rule for rounding a floating-point number + /// - Returns: The result after alignment + public func alignPixel(with rule: FloatingPointRoundingRule = .toNearestOrAwayFromZero) -> Base { + { (base * $0).rounded(rule) / $0 }(scale) + } }