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

Commit

Permalink
Fix Publishing changes from within view updates is not allowed, this …
Browse files Browse the repository at this point in the history
…will cause undefined behavior. (#25)

Co-authored-by: Andy Kayley <[email protected]>
  • Loading branch information
kaylanx and Andy Kayley authored Oct 28, 2022
1 parent 87e90ed commit 0e811cb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
10 changes: 7 additions & 3 deletions Sources/AttributedText/AttributedText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import SwiftUI

/// A view that displays styled attributed text.
public struct AttributedText: View {
@StateObject private var textSizeViewModel = TextSizeViewModel()
private var textSizeViewModel = TextSizeViewModel()
@State private var textSize: CGSize?

private let attributedText: NSAttributedString
private let onOpenLink: ((URL) -> Void)?
Expand Down Expand Up @@ -36,10 +37,13 @@ public struct AttributedText: View {
)
}
.frame(
idealWidth: textSizeViewModel.textSize?.width,
idealHeight: textSizeViewModel.textSize?.height
idealWidth: textSize?.width,
idealHeight: textSize?.height
)
.fixedSize(horizontal: false, vertical: true)
.onReceive(textSizeViewModel.$textSize) { size in
textSize = size
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/AttributedText/TextSizeViewModel.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import SwiftUI

final class TextSizeViewModel: ObservableObject {
final class TextSizeViewModel {
@Published var textSize: CGSize?

func didUpdateTextView(_ textView: AttributedTextImpl.TextView) {
Expand Down

0 comments on commit 0e811cb

Please sign in to comment.