Skip to content

Commit 963ea84

Browse files
authored
Merge pull request #164 from antoinelamy/feature/sizeToFit
Remove sizeToFit() from SizeCalculable protocol
2 parents 5c6097b + 181b5eb commit 963ea84

File tree

13 files changed

+74
-72
lines changed

13 files changed

+74
-72
lines changed

Example/PinLayoutSample.xcodeproj/project.pbxproj

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,6 @@
7575
remoteGlobalIDString = 249EFE831E64FB4C00165E39;
7676
remoteInfo = PinLayoutTests;
7777
};
78-
2439CC291E6658C3003326FB /* PBXContainerItemProxy */ = {
79-
isa = PBXContainerItemProxy;
80-
containerPortal = 2439CC1E1E665858003326FB /* PinLayout.xcodeproj */;
81-
proxyType = 1;
82-
remoteGlobalIDString = 249EFE791E64FB4C00165E39;
83-
remoteInfo = PinLayout;
84-
};
8578
24DA374E1EF7F90700D1AB2F /* PBXContainerItemProxy */ = {
8679
isa = PBXContainerItemProxy;
8780
containerPortal = 2439CC1E1E665858003326FB /* PinLayout.xcodeproj */;
@@ -111,7 +104,6 @@
111104
remoteInfo = "PinLayoutTests-tvOS";
112105
};
113106
/* End PBXContainerItemProxy section */
114-
115107
/* Begin PBXFileReference section */
116108
241637701F8E4BC200EE703A /* IntroObjectiveCViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IntroObjectiveCViewController.h; sourceTree = "<group>"; };
117109
241637711F8E4BC200EE703A /* IntroObjectiveCViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IntroObjectiveCViewController.m; sourceTree = "<group>"; };
@@ -499,7 +491,6 @@
499491
buildRules = (
500492
);
501493
dependencies = (
502-
2439CC2A1E6658C3003326FB /* PBXTargetDependency */,
503494
);
504495
name = PinLayoutSample;
505496
productName = PinLayoutSample;
@@ -716,14 +707,6 @@
716707
};
717708
/* End PBXSourcesBuildPhase section */
718709

719-
/* Begin PBXTargetDependency section */
720-
2439CC2A1E6658C3003326FB /* PBXTargetDependency */ = {
721-
isa = PBXTargetDependency;
722-
name = PinLayout;
723-
targetProxy = 2439CC291E6658C3003326FB /* PBXContainerItemProxy */;
724-
};
725-
/* End PBXTargetDependency section */
726-
727710
/* Begin PBXVariantGroup section */
728711
249EFE4B1E64FAFE00165E39 /* LaunchScreen.storyboard */ = {
729712
isa = PBXVariantGroup;

Example/PinLayoutSample/UI/Common/BasicView.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ class BasicView: UIView {
5151

5252
override func sizeThatFits(_ size: CGSize) -> CGSize {
5353
var newSize = CGSize()
54-
if size.width != CGFloat.greatestFiniteMagnitude {
54+
if size.width != CGFloat.greatestFiniteMagnitude && size.height != CGFloat.greatestFiniteMagnitude {
55+
newSize.width = 35
56+
newSize.height = 45
57+
} else if size.width != CGFloat.greatestFiniteMagnitude {
5558
newSize.width = size.width + sizeThatFitSizeOffset
5659
newSize.height = sizeThatFitsExpectedArea / newSize.width
5760
} else if size.height != CGFloat.greatestFiniteMagnitude {
@@ -64,8 +67,4 @@ class BasicView: UIView {
6467

6568
return newSize
6669
}
67-
68-
override func sizeToFit() {
69-
pin.width(35).height(45)
70-
}
7170
}

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ The resulting size will always respect [`minWidth`/`maxWidth`/`minHeight`/`maxHe
888888
**Methods:**
889889

890890
* **`sizeToFit()`**
891-
The method adjust the view's size based on the result of the method `UIView.sizeToFit()`. Particularly useful for controls/views that have an intrinsic size (label, button, ...).
891+
The method adjust the view's size based on it's content requirements so that it uses the most appropriate amount of space. This fit type has the same effect as calling **sizeToFit()** on a view. The resulting size come from sizeThatFits(..) being called with the current view bounds. Particularly useful for controls/views that have an intrinsic size (label, button, ...).
892892

893893
* **`sizeToFit(: FitType)`**
894894
The method adjust the view's size based on the result of the method `sizeThatFits(:CGSize)`.
@@ -1777,7 +1777,7 @@ PinLayout can layout **NSView**'s on macOS. All PinLayout's properties and metho
17771777

17781778
* PinLayout supports **only views that have a parent (superview) using a flipped coordinate system**, i.e. views for which the computed property `var isFlipped: Bool` returns true. In a flipped coordinate system, the origin is in the upper-left corner of the view and y-values extend downward. UIKit use this coordinate system. In a non-flipped coordinate system (default mode), the origin is in the lower-left corner of the view and positive y-values extend upward. See [Apple's documentation for more information about `NSView.isFlipped`](https://developer.apple.com/documentation/appkit/nsview/1483532-isflipped). The support of non-flipped coordinate system will be added soon.
17791779

1780-
* [`sizeToFit(:FitType)`](#sizeToFit) is supported only for instances that inherits from NSControl. Support for [`sizeToFit(:FitType)`](#sizeToFit) can be added to your custom NSView subclasses, just make those views conform to the `SizeCalculable` protocol and implement the two required functions.
1780+
* [`sizeToFit(:FitType)`](#sizeToFit) is supported only for instances that inherits from NSControl. Support for [`sizeToFit(:FitType)`](#sizeToFit) can be added to your custom NSView subclasses, just make those views conform to the `SizeCalculable` protocol and implement the required `sizeThatFits(:CGSize)` function.
17811781

17821782
* [`NSView.pin.safeArea` and ](#safeAreaInsets) property is not available, AppKit doesn't have an `UIView.safeAreaInsets` equivalent.
17831783

@@ -1793,7 +1793,7 @@ PinLayout can layout **NSView**'s on macOS. All PinLayout's properties and metho
17931793

17941794
PinLayout can layouts **CALayer**'s. All PinLayout's properties and methods are available, with the following exceptions:
17951795

1796-
* [`sizeToFit(:FitType)`](#sizeToFit) is not supported. Support for [`sizeToFit(:FitType)`](#sizeToFit) can be added to your custom CALayer subclasses, just make those layers conform to the `SizeCalculable` protocol and implement the two required functions.
1796+
* [`sizeToFit(:FitType)`](#sizeToFit) is not supported. Support for [`sizeToFit(:FitType)`](#sizeToFit) can be added to your custom CALayer subclasses, just make those layers conform to the `SizeCalculable` protocol and implement the required `sizeThatFits(:CGSize)` function.
17971797
* [`CALayer.pin.safeArea` and `CALayer.pin.readableMargins`](#safeAreaInsets) properties are not available.
17981798
* [`aspectRatio()`](#aspect_ratio) with no parameters
17991799

Sources/Impl/PinLayout+Layouting.swift

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,8 @@ extension PinLayout {
199199

200200
if let adjustSizeType = adjustSizeType {
201201
switch adjustSizeType {
202-
case .fitTypeWidth, .fitTypeHeight, .fitTypeWidthFlexible, .fitTypeHeightFlexible:
202+
case .fitTypeWidth, .fitTypeHeight, .fitTypeWidthFlexible, .fitTypeHeightFlexible, .fitTypeContent:
203203
size = computeSizeToFit(adjustSizeType: adjustSizeType, size: size)
204-
case .sizeToFit:
205-
size = computeSizeToFit(size: size)
206204
case .aspectRatio(let ratio):
207205
size = computeAspectRatio(ratio, size: size)
208206
}
@@ -236,17 +234,6 @@ extension PinLayout {
236234
return size
237235
}
238236

239-
private func computeSizeToFit(size: Size) -> Size {
240-
guard let sizeCalculableView = view as? SizeCalculable else {
241-
assertionFailure("Should not occurs, protocol conformance is checked before assigning adjustSizeType")
242-
return size
243-
}
244-
sizeCalculableView.sizeToFit()
245-
246-
let viewRect = view.getRect(keepTransform: keepTransform)
247-
return Size(width: viewRect.width, height: viewRect.height)
248-
}
249-
250237
private func computeSizeToFit(adjustSizeType: AdjustSizeType, size: Size) -> Size {
251238
guard let sizeCalculableView = view as? SizeCalculable else {
252239
assertionFailure("Should not occurs, protocol conformance is checked before assigning adjustSizeType")
@@ -271,22 +258,31 @@ extension PinLayout {
271258
} else {
272259
fitHeight = view.getRect(keepTransform: keepTransform).height
273260
}
261+
case .fitTypeContent:
262+
let fitSize = view.getRect(keepTransform: keepTransform).size
263+
fitWidth = fitSize.width
264+
fitHeight = fitSize.height
274265
default:
275266
assertionFailure("Should not occured")
276267
}
277268

278269
let sizeThatFits = sizeCalculableView.sizeThatFits(CGSize(width: fitWidth, height: fitHeight))
279270

280-
if fitWidth != .greatestFiniteMagnitude {
281-
size.width = adjustSizeType.isFlexible ? sizeThatFits.width : fitWidth
282-
} else {
283-
size.width = sizeThatFits.width
284-
}
271+
switch adjustSizeType {
272+
case .fitTypeWidth, .fitTypeWidthFlexible, .fitTypeHeight, .fitTypeHeightFlexible, .aspectRatio(_):
273+
if fitWidth != .greatestFiniteMagnitude {
274+
size.width = adjustSizeType.isFlexible ? sizeThatFits.width : fitWidth
275+
} else {
276+
size.width = sizeThatFits.width
277+
}
285278

286-
if fitHeight != .greatestFiniteMagnitude {
287-
size.height = adjustSizeType.isFlexible ? sizeThatFits.height : fitHeight
288-
} else {
289-
size.height = sizeThatFits.height
279+
if fitHeight != .greatestFiniteMagnitude {
280+
size.height = adjustSizeType.isFlexible ? sizeThatFits.height : fitHeight
281+
} else {
282+
size.height = sizeThatFits.height
283+
}
284+
case .fitTypeContent:
285+
size = Size(width: sizeThatFits.width, height: sizeThatFits.height)
290286
}
291287

292288
return size

Sources/ObjectiveC/PinLayoutObjC.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,8 @@ import AppKit
288288
@discardableResult func aspectRatio() -> PinLayoutObjC
289289
#endif
290290

291+
@discardableResult func sizeToFit() -> PinLayoutObjC
292+
291293
@discardableResult func sizeToFit(_ fitType: Fit) -> PinLayoutObjC
292294

293295
//
@@ -396,4 +398,5 @@ import AppKit
396398
case height
397399
case widthFlexible
398400
case heightFlexible
401+
case content
399402
}

Sources/ObjectiveC/PinLayoutObjCImpl.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,13 +679,18 @@ import AppKit
679679
}
680680
#endif
681681

682+
func sizeToFit() -> PinLayoutObjC {
683+
return sizeToFit(.content)
684+
}
685+
682686
func sizeToFit(_ fitType: Fit) -> PinLayoutObjC {
683687
let type: FitType
684688
switch fitType {
685689
case .width: type = .width
686690
case .height: type = .height
687691
case .widthFlexible: type = .widthFlexible
688692
case .heightFlexible: type = .heightFlexible
693+
case .content: type = .content
689694
}
690695
_ = impl?.sizeToFit(type)
691696
return self

Sources/PinLayout+Size.swift

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ enum AdjustSizeType {
2828
case fitTypeHeight
2929
case fitTypeWidthFlexible
3030
case fitTypeHeightFlexible
31-
32-
case sizeToFit
31+
case fitTypeContent
3332

3433
case aspectRatio(CGFloat)
3534

@@ -47,7 +46,7 @@ enum AdjustSizeType {
4746
switch self {
4847
case .fitTypeWidth, .fitTypeHeight,
4948
.fitTypeWidthFlexible, .fitTypeHeightFlexible,
50-
.sizeToFit:
49+
.fitTypeContent:
5150
return true
5251
case .aspectRatio(_):
5352
return false
@@ -72,6 +71,7 @@ extension FitType {
7271
case .height: return .fitTypeHeight
7372
case .widthFlexible: return .fitTypeWidthFlexible
7473
case .heightFlexible: return .fitTypeHeightFlexible
74+
case .content: return .fitTypeContent
7575
}
7676
}
7777
}
@@ -217,15 +217,9 @@ extension PinLayout {
217217
```
218218
*/
219219
@discardableResult
220-
public func sizeToFit(_ fitType: FitType) -> PinLayout {
220+
public func sizeToFit(_ fitType: FitType = .content) -> PinLayout {
221221
return setAdjustSizeType(fitType.toAdjustSizeType(), { return "sizeToFit(\(fitType.description))" })
222-
}
223-
224-
@discardableResult
225-
public func sizeToFit() -> PinLayout {
226-
return setAdjustSizeType(.sizeToFit, { return "sizeToFit()" })
227-
}
228-
}
222+
}}
229223

230224
//
231225
// MARK: Private methods
@@ -261,10 +255,8 @@ extension PinLayout {
261255
guard let adjustSizeType = adjustSizeType else { return }
262256
let conflict: String
263257
switch adjustSizeType {
264-
case .fitTypeWidth, .fitTypeHeight, .fitTypeWidthFlexible, .fitTypeHeightFlexible:
258+
case .fitTypeWidth, .fitTypeHeight, .fitTypeWidthFlexible, .fitTypeHeightFlexible, .fitTypeContent:
265259
conflict = "sizeToFit(\(adjustSizeType.toFitType()!.description))."
266-
case .sizeToFit:
267-
conflict = "sizeToFit()"
268260
case .aspectRatio(let ratio):
269261
conflict = "aspectRatio(\(ratio))"
270262
}

Sources/SizeCalculable.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,4 @@ import AppKit
2525

2626
public protocol SizeCalculable {
2727
func sizeThatFits(_ size: CGSize) -> CGSize
28-
func sizeToFit()
2928
}

Sources/Types+Description.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ extension FitType {
6868
case .height: return ".height"
6969
case .widthFlexible: return ".widthFlexible"
7070
case .heightFlexible: return ".heightFlexible"
71+
case .content: return ""
7172
}
7273
}
7374
}

Sources/Types.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,13 @@ public enum FitType {
176176
sizeThatFits(..) method result.
177177
*/
178178
case heightFlexible
179+
180+
/**
181+
Adjust the view's size based on it's content size requirements so that it uses the
182+
most appropriate amount of space. This fit type has the same effect as calling **sizeToFit()** on a view.
183+
The resulting size come from sizeThatFits(..) being called with the current view bounds.
184+
*/
185+
case content
179186
}
180187

181188
public enum WrapType {

0 commit comments

Comments
 (0)