-
-
Notifications
You must be signed in to change notification settings - Fork 132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamic Editor Height Question #33
Comments
I think this would be a bit tricky to achieve on all platforms, since the text views work a bit differently on iOS, where scrolling is part of the Would be nice to have though 👍 |
Not sure about macOS, but this can be done easily on iOS: struct MyTextView: UIViewRepresentable {
typealias UIViewType = UITextView
/* ... */
func sizeThatFits(_ proposal: ProposedViewSize, uiView: UITextView, context: Context) -> CGSize? {
// make sure we have a valid proposed width:
guard let proposedWidth = proposal.width, proposedWidth > 0, proposedWidth < .infinity else {
return nil
}
// calculate size of UITextView that fits current text:
var sizeThatFits = uiView.sizeThatFits(CGSize(
width: proposedWidth,
height: .greatestFiniteMagnitude
))
// fill parent horizontally:
sizeThatFits.width = proposedWidth
return sizeThatFits
}
} It's just an example of unconstrained, dynamic size. Constraining the minimum and maximum height should be straightforward. |
Thank you @darrarski! Did you get this to work @rydamckinney? |
Hey @danielsaidi Yes I was able to get this working, thought not in the context of RichTextKit. I needed markdown live syntax highlighting for the text input so I modified a fork of The solution should be generalizable to The rest of the code is pretty specific to the view hierarchy it's embedded in so I'm not sharing that so as to not confuse others (as I was led astray by many use-case-specific solutions I encountered in my research). Here's the crux of the logic: open class DynamicHeightNSTextView: NSTextView {
...
open override func didChangeText() {
super.didChangeText()
self.invalidateIntrinsicContentSize()
if let scrollHeight = self.scrollViewHeight {
NSAnimationContext.runAnimationGroup { context in
context.duration = 0.25
//scrollHeight is a constant NSLayoutConstraint for the containing scrollview's height
scrollHeight.constant = min(self.maxHeight, max(self.intrinsicContentSize.height, self.minHeight))
}
}
}
...
}
``` |
Thank you for sharing @rydamckinney! I will keep this issue open for when I have time to return to this project. |
This feature would be my dream! |
I have what I think is a simpler question but which has led me here -- how can I get the RichTextEditor to dynamically adjust its height based on the text content inside, so that it doesn't have to scroll for you to see all the text? I'm working in iOS (and SwiftUI). |
@slh-ideaflow I'd love to support this. I guess it'd involve calculating the editor frame and apply it as fixed height? |
Actually @danielsaidi , I think the @rydamckinney 's implementation above provides what I need, I just probably wasn't using it correctly before. I also found it helps to add a little padding to ensure the vertical scrolling won't be present:
|
@slh-ideaflow Thank you for sharing Sam! |
@rydamckinney Would adding that code to RichTextKit make it work right out of the box, or would it require additional changes? |
I'm sure you could figure out a better way to integrate this into the RichTextEditor... all that business with textView as a documentView of the NSScrollView... so I opted for this. It kind of makes sense to use as a different type because it's not really configurable. Like you can't just enable scrolling and then disable it, at least not the way I see it for NSTextView. Anyways, this works. I'll add some demos Screen.Recording.2025-01-09.at.6.56.41.PM.movSimulator.Screen.Recording.-.iPhone.16.Pro.-.2025-01-09.at.18.55.57.mp4 |
This looks amazing! 🤩 It would be great if this could be integrated with the already existing one, but I can imagine that it's tricky. I'll give it some time, or else merge it as a standalone view but perhaps not expose it directly in the SDK. |
It works great! SwiftUI scrollview even scrolls to the right place when you keep typing, which is very good. But I've seen your previous comments about how complicated it is with appkit non scrolling by default, and iOS scrolling by default... maybe the API being two separate views is actually a good thing because it communicates up front the complexity that you have mentioned. With all of that said, maybe the "Scrollable and NonScrollable" views are private, and the existing RichEditorView ends up being a wrapper with a parameter for embedInVerticalScrollView.. or an enum ContentContainerView.scroll, .resizingStatic? |
Is it currently possible to make the
RichTextEditor
frame dynamic with the following behavior?The text was updated successfully, but these errors were encountered: