简体   繁体   English

如何使NSTextView的高度始终高于实际20px的高度?

[英]How to make the height of NSTextView always higher than the actual height of 20px?

I'm using NSTextView as a text editor, and I want to make it about 20 PX taller than the recalculation after the user enters the text. 我将NSTextView用作文本编辑器,并且我希望使其比用户输入文本后的重新计算高20 PX。

That is, when NSTextView rolls in the end of the NSScrollView, there is a blank area that makes the user do not have to enter a return to make a blank line. 也就是说,当NSTextView在NSScrollView的末尾滚动时,将出现一个空白区域,该区域使用户不必输入回车符即可生成空白行。 In this way, the text of the tail will always be on the upper side to facilitate the user's visual experience. 这样,尾巴的文本将始终位于上侧,以方便用户的视觉体验。

How do you do this? 你怎么做到这一点?

How to resize NSTextView according to its content? 如何根据其内容调整NSTextView的大小?

I found some hints in this question, but the connection was invalid after too long. 我在此问题中找到了一些提示,但连接时间过长后无效。

Since your NSTextView is nested inside an NSScrolView most of the times, you can play with the scroll view's margins. 由于大多数时候您的NSTextView嵌套在NSScrolView内部,因此您可以使用滚动视图的边距。

The solution below is for storyboard-based app, which has a minium requirement of OS X 10.10 (Yosemite) : 以下解决方案适用于基于情节提要的应用程序,其最低要求为OS X 10.10(Yosemite)

class ViewController: NSViewController {
    @IBOutlet weak var scrollView: NSScrollView!
    @IBOutlet weak var textView: NSTextView!

    override func viewDidLoad() {
        super.viewDidLoad()
        textView.layoutManager?.delegate = self
        scrollView.automaticallyAdjustsContentInsets = false
    }
}

extension ViewController: NSLayoutManagerDelegate {
    func layoutManager(_ layoutManager: NSLayoutManager, didCompleteLayoutFor textContainer: NSTextContainer?, atEnd layoutFinishedFlag: Bool) {
        guard let textContainer = textContainer else { return }

        let textRect = layoutManager.usedRect(for: textContainer)
        let bottomSpace: CGFloat = 20.0
        let bottomInset: CGFloat = (textRect.height + bottomSpace) > scrollView.bounds.height ? bottomSpace : 0

        scrollView.contentInsets.bottom = bottomInset
        scrollView.scrollerInsets.bottom = -bottomInset
    }
}

Edit: how do I scroll the text view if I click on the bottom space? 编辑: 如果单击底部空间,如何滚动文本视图?

You can play around with NSParagraphStyle to add some after the last paragraph. 您可以NSParagraphStyle使用NSParagraphStyle在最后一段之后添加一些内容。 Or you can use the Responder Chain and make view controller change the cursor position to the end of the text view: 或者,您可以使用Responder Chain并使视图控制器将光标位置更改为文本视图的末尾:

class ViewController: NSViewController {
    // ...

    override func mouseUp(with event: NSEvent) {
        let bottomSpace = scrollView.contentInsets.bottom
        let clickLocation = self.view.convert(event.locationInWindow, to: textView)
        let bottomRect = CGRect(x: 0, y: textView.bounds.height, width: textView.bounds.width, height: bottomSpace)

        if bottomRect.contains(clickLocation) {
            textView.moveToEndOfDocument(self)
        }
    }
}

If you want multiple text views with the same behaviour, time to design your own class. 如果您希望多个文本视图具有相同的行为,请花时间设计自己的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM