Swiftpack.co -  SergeBouts/SeeMoreTextView as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
SergeBouts/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.
.package(url: "https://github.com/SergeBouts/SeeMoreTextView.git", from: "4.0.4")

Swift Platform SPM CocoaPods License

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.

License

This project is licensed under the MIT license.

GitHub

link
Stars: 16
Last commit: 3 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API