相关疑难解决方法(0)

增长NSTextView以适应内容是剪切最后一行文本

我正在尝试创建一个NSTextView,当用户键入并在高度达到最大值时滚动,它会垂直增长.这类似于Messages工作中的文本视图.

我的第一次尝试使用委托来监听文本更改并调整与NSTextView的滚动视图关联的高度约束:

- (void)textDidChange:(NSNotification *)notification
{
    NSTextView *textView = self.textView;
    NSRect usedRect = [textView.textContainer.layoutManager usedRectForTextContainer:textView.textContainer];
    NSLog(@"DEBUG: used rect: %@", NSStringFromRect(usedRect));
    self.textViewHeightConstraint.constant = MIN(80.f, MAX(usedRect.size.height, 30.f));
}
Run Code Online (Sandbox Code Playgroud)

这几乎可以工作:文本视图(滚动视图)的高度在我输入时更新,但是,最后一行文本被剪裁:

在此输入图像描述

一旦滚动视图达到它的最大高度并开始滚动它就可以很好地工作.我试图在封闭的滚动视图上强制显示/布局/约束更新,没有运气.我的猜测是滚动视图的剪辑视图没有正确更新,它正在剪切文本视图的底部.当约束发生变化时,有没有办法强制剪辑视图/滚动视图适当更新?

cocoa objective-c nstextview nsscrollview

5
推荐指数
1
解决办法
2046
查看次数

How to auto-expand height of NSTextView in SwiftUI?

How do I properly implement NSView constraints on the NSTextView below so it interacts with SwiftUI .frame()?

Goal

An NSTextView that, upon new lines, expands its frame vertically to force a SwiftUI parent view to render again (i.e., expand a background panel that's under the text + push down other content in VStack). The parent view is already wrapped in a ScrollView. Since the SwiftUI TextEditor is ugly and under-featured, I'm guessing several others new to MacOS will wonder how …

macos nstextview swiftui nsviewrepresentable

4
推荐指数
1
解决办法
4190
查看次数