Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 8281bdb

Browse files
committed
Refactor layout and fix macOS resize issue
1 parent 3e51554 commit 8281bdb

11 files changed

+214
-293
lines changed

Sources/AttributedText/AttributedText.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
#if canImport(SwiftUI) && !os(watchOS)
1+
#if canImport(SwiftUI) && !os(watchOS) && !targetEnvironment(macCatalyst)
22

33
import SwiftUI
44

55
@available(macOS 11.0, iOS 14.0, tvOS 14.0, *)
66
public struct AttributedText: View {
7-
@StateObject private var store = AttributedTextStore()
7+
@StateObject private var store = TextViewStore()
88

99
private let attributedText: NSAttributedString
1010

@@ -14,13 +14,17 @@
1414

1515
public var body: some View {
1616
GeometryReader { proxy in
17-
AttributedTextViewWrapper(attributedText: attributedText, store: store)
17+
TextViewWrapper(attributedText: attributedText, store: store)
1818
.preference(key: ContainerSizePreference.self, value: proxy.size)
1919
}
2020
.onPreferenceChange(ContainerSizePreference.self) { value in
2121
store.onContainerSizeChange(value)
2222
}
23-
.frame(height: store.height)
23+
.frame(
24+
idealWidth: store.intrinsicContentSize?.width,
25+
idealHeight: store.intrinsicContentSize?.height
26+
)
27+
.fixedSize(horizontal: false, vertical: true)
2428
}
2529
}
2630

@@ -29,7 +33,7 @@
2933
static var defaultValue: CGSize?
3034

3135
static func reduce(value: inout CGSize?, nextValue: () -> CGSize?) {
32-
value = value ?? nextValue()
36+
value = nextValue()
3337
}
3438
}
3539

Sources/AttributedText/AttributedTextStore.swift

Lines changed: 0 additions & 27 deletions
This file was deleted.

Sources/AttributedText/AttributedTextView+AppKit.swift

Lines changed: 0 additions & 104 deletions
This file was deleted.

Sources/AttributedText/AttributedTextView+UIKit.swift

Lines changed: 0 additions & 101 deletions
This file was deleted.

Sources/AttributedText/AttributedTextViewWrapper+AppKit.swift

Lines changed: 0 additions & 27 deletions
This file was deleted.

Sources/AttributedText/AttributedTextViewWrapper+UIKit.swift

Lines changed: 0 additions & 27 deletions
This file was deleted.

Sources/AttributedText/NSLineBreakMode+TruncationMode.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if canImport(SwiftUI) && !os(watchOS)
1+
#if canImport(SwiftUI) && !os(watchOS) && !targetEnvironment(macCatalyst)
22

33
import SwiftUI
44

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#if canImport(SwiftUI) && !os(watchOS) && !targetEnvironment(macCatalyst)
2+
3+
import SwiftUI
4+
5+
@available(macOS 11.0, iOS 14.0, tvOS 14.0, *)
6+
final class TextViewStore: ObservableObject {
7+
@Published var intrinsicContentSize: CGSize?
8+
9+
weak var view: TextViewWrapper.View?
10+
11+
func onContainerSizeChange(_ containerSize: CGSize?) {
12+
guard let containerSize = containerSize,
13+
let view = self.view else { return }
14+
15+
view.maxWidth = containerSize.width
16+
}
17+
18+
func didInvalidateIntrinsicContentSize() {
19+
guard let view = self.view else { return }
20+
21+
intrinsicContentSize = view.intrinsicContentSize
22+
}
23+
}
24+
25+
#endif

0 commit comments

Comments
 (0)