-
Notifications
You must be signed in to change notification settings - Fork 5
/
UIViewIdealSize.swift
58 lines (50 loc) · 1.86 KB
/
UIViewIdealSize.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//
// UIViewIdealSize.swift
//
//
// Created by Mikhail Apurin on 2021/10/27.
//
#if os(iOS)
import UIKit
/// Ideal size for the view. In Auto Layout terms, this is equivalent to the view intrinsic content size.
@available(iOS 13, *)
public struct IdealSize: Hashable {
/// Ideal width. When nil, the width of view's intrinsic content size will be used.
public let width: CGFloat?
/// Ideal height. When nil, the height of view's intrinsic content size will be used.
public let height: CGFloat?
public init(width: CGFloat?, height: CGFloat?) {
self.width = width
self.height = height
}
}
/// Calculation of the ideal size that fits the current size of the view.
@available(iOS 13, *)
public struct UIViewIdealSizeCalculator<Content: UIView> {
/// Calculate the view ideal size fot a given size.
public var viewIdealSizeInSize: (Content, CGSize) -> IdealSize
public init(viewIdealSizeInSize: @escaping (Content, CGSize) -> IdealSize) {
self.viewIdealSizeInSize = viewIdealSizeInSize
}
}
@available(iOS 13, *)
public extension UIViewIdealSizeCalculator {
/// Set the ideal size so that ideal width fits inside the proposed frame width. Ideal height is appropriate for the fitted width, but can exceed the proposed height.
static var `default`: Self {
.init { content, size in
let fittingSize = content.systemLayoutSizeFitting(
size,
withHorizontalFittingPriority: .required,
verticalFittingPriority: .fittingSizeLevel
)
return IdealSize(width: fittingSize.width, height: fittingSize.height)
}
}
/// Do not override the ideal size. Use intrinsic content size of the view.
static var none: Self {
.init { _, _ in
IdealSize(width: nil, height: nil)
}
}
}
#endif