From 72a9fcdbd7235ffc0204bbf7edb3bc76d25b483b Mon Sep 17 00:00:00 2001 From: Dominic Go Date: Sun, 6 Oct 2024 07:39:45 +0800 Subject: [PATCH] =?UTF-8?q?=E2=AD=90=EF=B8=8F=20Impl:=20`ViewPositionHoriz?= =?UTF-8?q?ontal+Helpers`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ios/Temp/String+Helpers.swift | 29 ++++++++++++ ios/Temp/UIView+Helpers.swift | 41 +++++++++++++++++ ios/Temp/ViewPositionHorizontal.swift | 44 ++++++++++++++++++- .../ViewPositionHorizontalIdentifiers.swift | 26 +++++++++++ 4 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 ios/Temp/String+Helpers.swift create mode 100644 ios/Temp/UIView+Helpers.swift create mode 100644 ios/Temp/ViewPositionHorizontalIdentifiers.swift diff --git a/ios/Temp/String+Helpers.swift b/ios/Temp/String+Helpers.swift new file mode 100644 index 0000000..1cac199 --- /dev/null +++ b/ios/Temp/String+Helpers.swift @@ -0,0 +1,29 @@ +// +// String+Helpers.swift +// react-native-ios-modal +// +// Created by Dominic Go on 10/6/24. +// + +import Foundation + + +public extension String { + + var capitalizedFirstLetter: Self { + var copy = self; + copy.capitalizeFirstLetter(); + return copy; + }; + + mutating func capitalizeFirstLetter() { + guard let firstLetter = self.first else { + return; + }; + + self.replaceSubrange( + ...self.startIndex, + with: firstLetter.uppercased() + ); + }; +}; diff --git a/ios/Temp/UIView+Helpers.swift b/ios/Temp/UIView+Helpers.swift new file mode 100644 index 0000000..1bc054d --- /dev/null +++ b/ios/Temp/UIView+Helpers.swift @@ -0,0 +1,41 @@ +// +// UIView+Helpers.swift +// react-native-ios-modal +// +// Created by Dominic Go on 10/6/24. +// + +import Foundation +import DGSwiftUtilities + + +public extension UIView { + + func recursivelyFindConstraint( + withIdentifier identifier: String + ) -> NSLayoutConstraint? { + + var indexOfConstraintInView: Int?; + + let match = self.recursivelyFindSubview { + let match = $0.constraints.enumerated().first { + $0.element.identifier == identifier; + }; + + guard let match = match else { + return false; + }; + + indexOfConstraintInView = match.offset; + return true; + }; + + guard let match = match, + let indexOfConstraintInView = indexOfConstraintInView + else { + return nil; + }; + + return match.constraints[indexOfConstraintInView]; + }; +}; diff --git a/ios/Temp/ViewPositionHorizontal.swift b/ios/Temp/ViewPositionHorizontal.swift index 2917f5c..3b0a088 100644 --- a/ios/Temp/ViewPositionHorizontal.swift +++ b/ios/Temp/ViewPositionHorizontal.swift @@ -6,9 +6,15 @@ // import Foundation - +import DGSwiftUtilities public enum ViewPositionHorizontal { + + public typealias Identifier = ViewPositionHorizontalIdentifier; + + // MARK: - Case Members + // -------------------- + case stretch; case stretchPercent(percent: CGFloat); @@ -357,3 +363,39 @@ public enum ViewPositionHorizontal { return constraints; }; }; + +// MARK: ViewPositionHorizontal+Helpers +// ------------------------------------ + +public extension ViewPositionHorizontal { + + func findConstraint( + inConstraints constraints: [NSLayoutConstraint], + withIdentifier identifierPreset: Identifier + ) -> NSLayoutConstraint? { + + constraints.first { + $0.identifier == identifierPreset.identifier; + }; + }; + + func findConstraint( + inView view: UIView, + withIdentifier identifierPreset: Identifier + ) -> NSLayoutConstraint? { + + view.constraints.first { + $0.identifier == identifierPreset.identifier; + }; + }; + + func recursivelyFindConstraint( + inView view: UIView, + withIdentifier identifierPreset: Identifier + ) -> NSLayoutConstraint? { + + view.recursivelyFindConstraint( + withIdentifier: identifierPreset.identifier + ); + }; +}; diff --git a/ios/Temp/ViewPositionHorizontalIdentifiers.swift b/ios/Temp/ViewPositionHorizontalIdentifiers.swift new file mode 100644 index 0000000..ed5559d --- /dev/null +++ b/ios/Temp/ViewPositionHorizontalIdentifiers.swift @@ -0,0 +1,26 @@ +// +// ViewPositionHorizontalIdentifiers.swift +// react-native-ios-modal +// +// Created by Dominic Go on 10/6/24. +// + +import Foundation +import DGSwiftUtilities + +public enum ViewPositionHorizontalIdentifier: String { + case leading; + case trailing; + case width; + case centerX; + case leadingMin; + case trailingMin; + + public var identifier: String { + let prefix = "generatedHorizontalPosition"; + let suffix = "Constraint"; + + let center = self.rawValue.capitalizedFirstLetter; + return prefix + center + suffix; + }; +};