How to cut the long text until it fits NSTextView and also add ... in the end of cut text?

Viewed 264

How can I cut the text until it fits NSTextView? Exactly until it fits. And also then add ... in the end of this cut text?

Here is my component:

import PureLayout

class Card: NSCollectionViewItem {
    let textView = NSTextView()

    var card: (text: String)? {
        didSet {
            guard isViewLoaded else { return }

            if let text = card?.text {
                textView.string = text
            } else {
                textView.string = ""
            }
        }
    }

    func configureTextView() {
        textView.font = NSFont(name: "SF Pro Display", size: 12)
        textView.isEditable = false
        textView.isFieldEditor = false

        textView.autoPinEdge(toSuperviewEdge: .top, withInset: 10)
        textView.autoPinEdge(toSuperviewEdge: .right, withInset: 10)
        textView.autoPinEdge(toSuperviewEdge: .bottom, withInset: 10)
        textView.autoPinEdge(toSuperviewEdge: .left, withInset: 10)
    }

    override func viewDidAppear() {
        super.viewDidAppear()

        view.layer?.borderWidth = 1
        view.layer?.borderColor = .black

        view.addSubview(textView)

        configureTextView()
    }
}

Here is an example of the entry text:

enter image description here

Here is how it should look like in the NSTextView after it was cut:

enter image description here

1 Answers
Related