diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f6f31f..2a834ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ #CHANGELOG +##3.0.1 + +- Swift 4.2 support +- use `utf16` symbols count to place more & less buttons correctly in the HTML page. + ##3.0.0 - Swift 4 support diff --git a/ReadMoreTextView.podspec b/ReadMoreTextView.podspec index 5e6143e..7d855f6 100644 --- a/ReadMoreTextView.podspec +++ b/ReadMoreTextView.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = "ReadMoreTextView" - s.version = "3.0.0" + s.version = "3.0.1" s.summary = 'UITextView subclass with "read more"/"read less" capabilities and UITextView extensions to handle touches in characters range.' s.description = <<-DESC diff --git a/Sources/ReadMoreTextView.swift b/Sources/ReadMoreTextView.swift index b32f98c..83745e4 100644 --- a/Sources/ReadMoreTextView.swift +++ b/Sources/ReadMoreTextView.swift @@ -47,8 +47,8 @@ public class ReadMoreTextView: UITextView { isEditable = false let attributedDefaultReadMoreText = NSAttributedString(string: defaultReadMoreText, attributes: [ - NSAttributedStringKey.foregroundColor: UIColor.lightGray, - NSAttributedStringKey.font: font ?? UIFont.systemFont(ofSize: 14) + NSAttributedString.Key.foregroundColor: UIColor.lightGray, + NSAttributedString.Key.font: font ?? UIFont.systemFont(ofSize: 14) ]) attributedReadMoreText.append(attributedDefaultReadMoreText) self.attributedReadMoreText = attributedReadMoreText @@ -198,7 +198,7 @@ public class ReadMoreTextView: UITextView { public override var intrinsicContentSize : CGSize { textContainer.size = CGSize(width: bounds.size.width, height: CGFloat.greatestFiniteMagnitude) var intrinsicContentSize = layoutManager.boundingRect(forGlyphRange: layoutManager.glyphRange(for: textContainer), in: textContainer).size - intrinsicContentSize.width = UIViewNoIntrinsicMetric + intrinsicContentSize.width = UIView.noIntrinsicMetric intrinsicContentSize.height += (textContainerInset.top + textContainerInset.bottom) intrinsicContentSize.height = ceil(intrinsicContentSize.height) return intrinsicContentSize @@ -237,8 +237,8 @@ public class ReadMoreTextView: UITextView { private func attributedStringWithDefaultAttributes(from text: String) -> NSAttributedString { return NSAttributedString(string: text, attributes: [ - NSAttributedStringKey.font: font ?? UIFont.systemFont(ofSize: 14), - NSAttributedStringKey.foregroundColor: textColor ?? UIColor.black + NSAttributedString.Key.font: font ?? UIFont.systemFont(ofSize: 14), + NSAttributedString.Key.foregroundColor: textColor ?? UIColor.black ]) } @@ -274,7 +274,7 @@ public class ReadMoreTextView: UITextView { if let originalAttributedText = _originalAttributedText?.mutableCopy() as? NSMutableAttributedString { attributedText = _originalAttributedText - let range = NSRange(location: 0, length: text.count) + let range = NSRange(location: 0, length: text.length) if let attributedReadLessText = attributedReadLessText { originalAttributedText.append(attributedReadLessText) } @@ -323,7 +323,7 @@ public class ReadMoreTextView: UITextView { let characterIndex = layoutManager.characterIndexForGlyph(at: glyphIndex) return characterIndex - 1 } else { - return NSMaxRange(rangeThatFits) - readMoreText!.count + return NSMaxRange(rangeThatFits) - readMoreText!.length } } @@ -333,20 +333,20 @@ public class ReadMoreTextView: UITextView { let layoutManager = NSLayoutManager() layoutManager.addTextContainer(textContainer) textStorage.addLayoutManager(layoutManager) - let readMoreBoundingRect = layoutManager.boundingRectForCharacterRange(range: NSMakeRange(0, text.count), inTextContainer: textContainer) + let readMoreBoundingRect = layoutManager.boundingRectForCharacterRange(range: NSMakeRange(0, text.length), inTextContainer: textContainer) return readMoreBoundingRect } private func readMoreTextRange() -> NSRange { var readMoreTextRange = rangeToReplaceWithReadMoreText() if readMoreTextRange.location != NSNotFound { - readMoreTextRange.length = readMoreText!.count + 1 + readMoreTextRange.length = readMoreText!.length + 1 } return readMoreTextRange } private func readLessTextRange() -> NSRange { - return NSRange(location: _originalTextLength, length: readLessText!.count + 1) + return NSRange(location: _originalTextLength, length: readLessText!.length + 1) } private func pointIsInReadMoreOrReadLessTextRange(point aPoint: CGPoint) -> Bool? { @@ -357,5 +357,11 @@ public class ReadMoreTextView: UITextView { } return nil } - + +} + +extension String { + var length: Int { + return utf16.count + } } diff --git a/Sources/UITextView+Extensions.swift b/Sources/UITextView+Extensions.swift index 310b329..a44ecbb 100644 --- a/Sources/UITextView+Extensions.swift +++ b/Sources/UITextView+Extensions.swift @@ -19,7 +19,7 @@ extension UITextView { guard let charIndex = charIndexForPointInGlyphRect(point: aPoint) else { return super.hitTest(aPoint, with: event) } - guard textStorage.attribute(NSAttributedStringKey.link, at: charIndex, effectiveRange: nil) == nil else { + guard textStorage.attribute(NSAttributedString.Key.link, at: charIndex, effectiveRange: nil) == nil else { return super.hitTest(aPoint, with: event) } return test(charIndex)