From e6e6ef851aa149a0fd5ae985b6e06e7b539f51bb Mon Sep 17 00:00:00 2001 From: Ilya Puchka Date: Thu, 12 Jan 2017 21:50:39 +0100 Subject: [PATCH] updated docs --- CHANGELOG.md | 5 +++ README.md | 52 ++++++++++++++++++++++++++--- ReadMoreTextView.podspec | 8 +++-- Sources/UITextView+Extensions.swift | 2 +- 4 files changed, 59 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a11b77b..0425851 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ #CHANGELOG +##1.2.0 + +- fixed loosing data detector info when updating text +- moved helper methods to public UITextView extension to easily add custom handling for toucher in arbitrary text ranges + ##1.1.0 - added `setNeedsUpdateTrim` method to force update trimming diff --git a/README.md b/README.md index 412e820..ce30a30 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ # ReadMoreTextView -UITextView subclass with "read more"/"read less" capabilities. +UITextView subclass with "read more"/"read less" capabilities and UITextView extensions to handle touches in characters range. ![](screenshot.gif) @@ -17,15 +17,59 @@ UITextView subclass with "read more"/"read less" capabilities. textView.shouldTrim = true textView.maximumNumberOfLines = 4 - textView.readMoreText = "... Read more" - textView.readLessText = " Read less" + textView.attributedReadMoreText = NSAttributedString(string: "... Read more") + textView.attributedReadMoreText = 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. + +```swift + +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](https://github.com/CocoaPods/CocoaPods): - pod 'ReadMoreTextView' +` pod 'ReadMoreTextView' ` ##License diff --git a/ReadMoreTextView.podspec b/ReadMoreTextView.podspec index eebbf35..66baba2 100644 --- a/ReadMoreTextView.podspec +++ b/ReadMoreTextView.podspec @@ -1,16 +1,18 @@ Pod::Spec.new do |s| s.name = "ReadMoreTextView" - s.version = "1.1.0" - s.summary = 'UITextView subclass with "read more"/"read less" capabilities.' + s.version = "1.2.0" + s.summary = 'UITextView subclass with "read more"/"read less" capabilities and UITextView extensions to handle touches in characters range.' s.description = <<-DESC UITextView subclass with "Read more" behavior. + UITextView extensions to handle touches in characters range. * Set "read more"/"read less" text as a String or NSAttributedString * Set maximum number of lines to display * Turn trim on/off - * Live updates and setup in Interface Builder + * Live updates and setup in Interface Builder + * Use UITextView extension methods to detect touches in arbitrary text ranges. DESC s.homepage = "http://ilya.puchka.me/custom-uitextview-in-swift/" diff --git a/Sources/UITextView+Extensions.swift b/Sources/UITextView+Extensions.swift index 5f6bca6..2bff3c2 100644 --- a/Sources/UITextView+Extensions.swift +++ b/Sources/UITextView+Extensions.swift @@ -13,7 +13,7 @@ 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. + Return `self` in `test` if text view should intercept the touch event or `nil` otherwise. */ public func hitTest(pointInGliphRange aPoint: CGPoint, event: UIEvent?, test: (Int) -> UIView?) -> UIView? { guard let charIndex = charIndexForPointInGlyphRect(point: aPoint) else {