SeeMoreTextView

NSTextView/UITextView subclass with an expandable See More link. It helps avoid cluttering with too much information, by hiding some of it, allowing users to choose whether to display the hidden information. The hidden information is displayed by clicking on the See More link.

Advantages

The main advantage of SeeMoreTextView is that it preserves text model intact fully accomplishing its functionality with the layout manager.

How

import SeeMoreTextView
...
var seeMoreTextView: SeeMoreTextView!
var scrollViewHeightConstraint: NSLayoutConstraint!

override func viewWillAppear() {
    super.viewWillAppear()

    seeMoreTextView = SeeMoreTextView(frame: .zero)
    seeMoreTextView.translatesAutoresizingMaskIntoConstraints = false
    
    let scrollView = CustomScrollView()
    view.addSubview(scrollView)
    scrollView.translatesAutoresizingMaskIntoConstraints = false
    scrollView.borderType = .noBorder
    scrollView.hasVerticalScroller = true
    scrollView.hasHorizontalScroller = false
    scrollView.documentView = seeMoreTextView
    
    scrollViewHeightConstraint = scrollView.heightAnchor.constraint(equalToConstant: 0)
    // See more text view's height change hook, which can be used for custom layout handling
    seeMoreTextView.onHeightChange = { sender in
        self.scrollViewHeightConstraint.constant = min(sender.height, 100)
        (sender.enclosingScrollView as! CustomScrollView).isEnabled = sender.isExpanded
    }
    
    NSLayoutConstraint.activate([
        scrollView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
        scrollView.topAnchor.constraint(equalTo: view.topAnchor, constant: 10),
        scrollViewHeightConstraint,
        scrollView.widthAnchor.constraint(equalTo: seeMoreTextView.widthAnchor),
        scrollView.widthAnchor.constraint(equalToConstant: 400),
    ])

    // Full text to display
    let str = "On the other hand, ...."
    seeMoreTextView.contents = NSAttributedString(string: str, attributes: [
            .font: NSFontManager.shared.font(withFamily: "Helvetica", traits: [], weight: 5, size: 14)!
        ])
    // Number of lines to display in collapsed state
    seeMoreTextView.collapsedLineCount = 2
}

// NSScrollView subclass disabling scrolling on demand
class CustomScrollView: NSScrollView {
    var isEnabled = false
    override func scrollWheel(with event: NSEvent) {
        if isEnabled {
            super.scrollWheel(with: event)
        } else {
            nextResponder?.scrollWheel(with: event)
        }
    }
}

Installation

Swift Package as dependency in Xcode 11+

  1. Go to "File" -> "Swift Packages" -> "Add Package Dependency"
  2. Paste SeeMoreTextView repository URL into the search field:

https://github.com/SergeBouts/SeeMoreTextView.git

  1. Click "Next"

  2. Ensure that the "Rules" field is set to something like this: "Version: Up To Next Major: 3.0.6"

  3. Click "Next" to finish

For more info, check out here.

CocoaPods

Add the following to your Podfile:

platform :osx, '10.12'

target 'YOUR-TARGET' do
  use_frameworks!
  pod 'SeeMoreTextView', :git => 'https://github.com/SergeBouts/SeeMoreTextView.git'
end

Then run pod install.

GitHub