Skip to content
UITextView subclass with "read more"/"read less" capabilities
Swift Ruby
Branch: master
Clone or download

Latest commit

ilyapuchka Merge pull request #50 from dchohfi/master
Fix problem with string length and composed emojis
Latest commit 9a855a8 Jul 11, 2019

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Example Add sample to handle composed emojis Jul 10, 2019
Sources Normalize usage of string length to fix composed emojis Jul 10, 2019
.gitignore Initial commit Apr 4, 2015
.swift-version added .swift-version file Oct 3, 2017
CHANGELOG.md Swift 4.2 support Nov 1, 2018
LICENSE updated licence Nov 26, 2016
README.md Update README.md Nov 16, 2018
ReadMoreTextView.podspec Swift 4.2 support Nov 1, 2018
screenshot.gif replaced screenshot with gif Dec 4, 2016

README.md

No Maintenance Intended Version License Platform Swift Version

ReadMoreTextView

UITextView subclass with "read more"/"read less" capabilities and UITextView extensions to handle touches in characters range.

Usage

let textView = ReadMoreTextView()

textView.text = "Lorem ipsum dolor ..."

textView.shouldTrim = true
textView.maximumNumberOfLines = 4
textView.attributedReadMoreText = NSAttributedString(string: "... Read more")
textView.attributedReadLessText = NSAttributedString(string: " Read less")

Custom touches handling.

This project also includes few helper methods that will allow you to detect and handle touches in arbitrary text ranges. This way you can for exapmle implement custom links handling in your UITextView subclass. ReadMoreTextView uses these methods itself to detect touches in "read more"/"read less" action areas.

extension UITextView {

	/**
	 Calls provided `test` block if point is in gliph range and there is no link detected at this point.
	 Will pass in to `test` a character index that corresponds to `point`.
	 Return `self` in `test` if text view should intercept the touch event or `nil` otherwise.
	 */
	public func hitTest(pointInGliphRange:event:test:) -> UIView?
	
	/**
	 Returns true if point is in text bounding rect adjusted with padding.
	 Bounding rect will be enlarged with positive padding values and decreased with negative values.
	 */
	public func pointIsInTextRange(point:range:padding:) -> Bool
	
	/**
	 Returns index of character for glyph at provided point. Returns `nil` if point is out of any glyph.
	 */
	public func charIndexForPointInGlyphRect(point:) -> Int?
	
}

extension NSLayoutManager {

    /**
     Returns characters range that completely fits into container.
     */
    public func characterRangeThatFits(textContainer:) -> NSRange
    
    /**
     Returns bounding rect in provided container for characters in provided range.
     */
    public func boundingRectForCharacterRange(range:inTextContainer:) -> CGRect

}

Installation

Available in Cocoa Pods:

pod 'ReadMoreTextView'

License

ReadMoreTextView is available under the MIT license.

You can’t perform that action at this time.