UITextView subclass with "read more"/"read less" capabilities
Swift Ruby
Switch branches/tags
Clone or download
Latest commit 7efd82c Apr 6, 2018

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.