Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add alignPixel func to CGFloat extension #15

Merged
merged 2 commits into from
May 7, 2024
Merged
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
22 changes: 15 additions & 7 deletions Sources/Core/Extensions/CGFloat+RAK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,27 @@

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
}

/// 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)
}
}
1 change: 1 addition & 0 deletions Sources/Core/Extensions/Extendable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 { }
Expand Down