Closed CooperHash closed 1 year ago
Actually, you can use SwiftUI's native way to get view height.
MarkdownView(text: document.text, baseURL: URL(filePath: baseURL))
.overlay {
GeometryReader { proxy in
Color.clear
.task(id: proxy.size.height) {
print("height: \(proxy.size.height)")
}
}
}
This value can be dynamic when size changes.
https://github.com/LiYanan2004/MarkdownView/assets/37542129/bfcde9ec-4d98-4288-9be7-54771cc3c0f2
thanks for advice which works in my project. the idea of this issue from MarkdownView
import SwiftUI
import MarkdownView
struct SampleUI: View {
var body: some View {
ScrollView {
MarkdownUI(body: markdown)
.onTouchLink { link in
print(link)
return false
}
.onRendered { height in
print(height)
}
}
}
private var markdown: String {
let path = Bundle.main.path(forResource: "sample", ofType: "md")!
let url = URL(fileURLWithPath: path)
return try! String(contentsOf: url, encoding: String.Encoding.utf8)
}
}
thanks for advice which works in my project.
That's good.
Adding an overlay is a better solution. Whenever the height of the view changes, for example, device rotates, it can notify you immediately. These scenarios will not trigger re-render because markdown content hasn't changed, so onRendered
will not be called again, which may cause unexpected behavior. So I think onRendered(action: (_ height: CGFloat) -> Void)
is useless.
The feature you want to get in MarkdownView get md block height like MarkdownView(text: "xxx")..onRendered { height in print(height) }
Additional context in some case, we need to know the height of MarkdownView, so i wonder if we can add a new api