From 408516e7868b5dde55e89f8eef08732ad5966f21 Mon Sep 17 00:00:00 2001 From: Nikita Ermolenko Date: Wed, 13 Dec 2017 09:08:07 +0600 Subject: [PATCH 1/5] Remove workspace --- Framezilla.xcworkspace/contents.xcworkspacedata | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 Framezilla.xcworkspace/contents.xcworkspacedata diff --git a/Framezilla.xcworkspace/contents.xcworkspacedata b/Framezilla.xcworkspace/contents.xcworkspacedata deleted file mode 100644 index 88e3ffc..0000000 --- a/Framezilla.xcworkspace/contents.xcworkspacedata +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - From 7f54769eee37458e2b645b0192560f4128eb3c7d Mon Sep 17 00:00:00 2001 From: Nikita Ermolenko Date: Wed, 13 Dec 2017 09:23:41 +0600 Subject: [PATCH 2/5] Improve performance of the frame configuration - ContiguousArray --- Sources/Maker.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Maker.swift b/Sources/Maker.swift index 1d7e407..4968919 100644 --- a/Sources/Maker.swift +++ b/Sources/Maker.swift @@ -25,7 +25,7 @@ public final class Maker { unowned let view: UIView - var handlers: [(priority: HandlerPriority, handler: HandlerType)] = [] + var handlers = ContiguousArray<(priority: HandlerPriority, handler: HandlerType)>() var newRect: CGRect private var widthParameter: ValueParameter? From 85eaccf8fbd98c7da9446a08eb99e9ff3d464b92 Mon Sep 17 00:00:00 2001 From: Nikita Ermolenko Date: Wed, 13 Dec 2017 09:26:38 +0600 Subject: [PATCH 3/5] Expand the extension of configureFrames and stack to `Sequence` and `Collection` --- Sources/Array+Stack.swift | 2 +- Sources/UIView+Installer.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/Array+Stack.swift b/Sources/Array+Stack.swift index 5699420..f953b06 100644 --- a/Sources/Array+Stack.swift +++ b/Sources/Array+Stack.swift @@ -11,7 +11,7 @@ public enum StackAxis: Int { case vertical } -public extension Array where Element: UIView { +public extension Collection where Iterator.Element: UIView, Self.Index == Int, Self.IndexDistance == Int { /// Arranges views in the order of list along a vertical or horizontal axis, with spacing property. /// diff --git a/Sources/UIView+Installer.swift b/Sources/UIView+Installer.swift index 827c888..2c4d5c8 100644 --- a/Sources/UIView+Installer.swift +++ b/Sources/UIView+Installer.swift @@ -86,7 +86,7 @@ public extension UIView { } } -public extension Array where Element: UIView { +public extension Sequence where Iterator.Element: UIView { /// Configures frames of the views for special state. /// From 4dd60d23c3f60c2409ac144c7978cb36f50647e9 Mon Sep 17 00:00:00 2001 From: Nikita Ermolenko Date: Mon, 25 Dec 2017 12:45:17 +0600 Subject: [PATCH 4/5] Add `widthThatFits` and `heightThatFits` (#9) --- Sources/Maker.swift | 54 +++++++++++++++------- Tests/MakerWidthHeightTests.swift | 76 +++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 17 deletions(-) diff --git a/Sources/Maker.swift b/Sources/Maker.swift index 4968919..334112c 100644 --- a/Sources/Maker.swift +++ b/Sources/Maker.swift @@ -391,7 +391,25 @@ public final class Maker { setHighPriorityValue(view.bounds.height, for: .height) return self } - + + /// Calculates the size that best fits the specified size. + /// + /// ``` + /// maker.sizeThatFits(size: CGSize(width: cell.frame.width, height: cell.frame.height) + /// ``` + /// - parameter size: The size for best-fitting. + /// + /// - returns: `Maker` instance for chaining relations. + + @discardableResult public func sizeThatFits(size: CGSize) -> Maker { + let fitSize = view.sizeThatFits(size) + let width = min(size.width, fitSize.width) + let height = min(size.height, fitSize.height) + setHighPriorityValue(width, for: .width) + setHighPriorityValue(height, for: .height) + return self + } + /// Resizes and moves the receiver view so it just encloses its subviews only for height. /// /// - returns: `Maker` instance for chaining relations. @@ -401,7 +419,17 @@ public final class Maker { setHighPriorityValue(view.bounds.height, for: .height) return self } - + + /// Calculates the height that best fits the specified size. + /// + /// - returns: `Maker` instance for chaining relations. + + @discardableResult public func heightThatFits(height: Number) -> Maker { + view.sizeToFit() + setHighPriorityValue(min(view.bounds.height, height.value), for: .height) + return self + } + /// Resizes and moves the receiver view so it just encloses its subviews only for width. /// /// - returns: `Maker` instance for chaining relations. @@ -411,25 +439,17 @@ public final class Maker { setHighPriorityValue(view.bounds.width, for: .width) return self } - - /// Calculates the size that best fits the specified size. - /// - /// ``` - /// maker.sizeThatFits(size: CGSize(width: cell.frame.width, height: cell.frame.height) - /// ``` - /// - parameter size: The size for best-fitting. + + /// Calculates the width that best fits the specified size. /// /// - returns: `Maker` instance for chaining relations. - - @discardableResult public func sizeThatFits(size: CGSize) -> Maker { - let fitSize = view.sizeThatFits(size) - let width = min(size.width, fitSize.width) - let height = min(size.height, fitSize.height) - setHighPriorityValue(width, for: .width) - setHighPriorityValue(height, for: .height) + + @discardableResult public func widthThatFits(width: Number) -> Maker { + view.sizeToFit() + setHighPriorityValue(min(view.bounds.width, width.value), for: .width) return self } - + // MARK: Middle priority /// Creates margin relation for superview. diff --git a/Tests/MakerWidthHeightTests.swift b/Tests/MakerWidthHeightTests.swift index 97cff57..cedaa18 100644 --- a/Tests/MakerWidthHeightTests.swift +++ b/Tests/MakerWidthHeightTests.swift @@ -154,4 +154,80 @@ class MakerWidthHeightTests: BaseTest { } XCTAssertEqual(testingView.frame, CGRect(x: 125, y: 187.5, width: 250, height: 125)) } + + /* width / height to fit */ + + func testWidthToFit() { + + let label = UILabel() + label.text = "HelloHelloHelloHello" + + label.configureFrame { maker in + maker.widthToFit() + } + XCTAssertTrue(label.bounds.width > 0) + XCTAssertEqual(label.bounds.height, 0) + } + + func testHeightToFit() { + + let label = UILabel() + label.text = "HelloHelloHelloHello" + + label.configureFrame { maker in + maker.heightToFit() + } + XCTAssertTrue(label.bounds.height > 0) + XCTAssertEqual(label.bounds.width, 0) + } + + /* width / height that fits */ + + func testThat_widthThatFits_correctlyConfiguresRelativeLowMaxWidth() { + + let label = UILabel() + label.text = "HelloHelloHelloHello" + + label.configureFrame { maker in + maker.widthThatFits(width: 30) + } + XCTAssertEqual(label.bounds.width, 30) + XCTAssertEqual(label.bounds.height, 0) + } + + func testThat_widthThatFits_correctlyConfiguresRelativeHighMaxWidth() { + + let label = UILabel() + label.text = "HelloHelloHelloHello" + + label.configureFrame { maker in + maker.widthThatFits(width: 300) + } + XCTAssertTrue(label.bounds.width != 300) + XCTAssertEqual(label.bounds.height, 0) + } + + func testThat_heightThatFits_correctlyConfiguresRelativeLowMaxHeight() { + + let label = UILabel() + label.text = "HelloHelloHelloHello" + + label.configureFrame { maker in + maker.heightThatFits(height: 5) + } + XCTAssertEqual(label.bounds.height, 5) + XCTAssertEqual(label.bounds.width, 0) + } + + func testThat_heightThatFits_correctlyConfiguresRelativeHighMaxHeight() { + + let label = UILabel() + label.text = "HelloHelloHelloHello" + + label.configureFrame { maker in + maker.heightThatFits(height: 300) + } + XCTAssertTrue(label.bounds.height != 300) + XCTAssertEqual(label.bounds.width, 0) + } } From 6d7296901f3fffa20d5dea2358b724edf4498bce Mon Sep 17 00:00:00 2001 From: Nikita Ermolenko Date: Mon, 25 Dec 2017 15:27:08 +0600 Subject: [PATCH 5/5] Change the podspec version -> 2.5.0 --- Framezilla.podspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Framezilla.podspec b/Framezilla.podspec index 46bcb93..d75a0fa 100644 --- a/Framezilla.podspec +++ b/Framezilla.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |spec| spec.name = "Framezilla" - spec.version = "2.4.1" + spec.version = "2.5.0" spec.summary = "Comfortable syntax for working with frames." spec.homepage = "https://github.com/Otbivnoe/Framezilla"