Skip to content
This repository has been archived by the owner on Jun 17, 2021. It is now read-only.

Commit

Permalink
Merge pull request #45 from KovtunOleg/master
Browse files Browse the repository at this point in the history
Use `utf16` symbols count to place more & less buttons correctly in the HTML page.
  • Loading branch information
ilyapuchka authored Nov 10, 2018
2 parents 7efd82c + f04d78c commit 46f8e0a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion ReadMoreTextView.podspec
Original file line number Diff line number Diff line change
@@ -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
Expand Down
28 changes: 17 additions & 11 deletions Sources/ReadMoreTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
])
}

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
}

Expand All @@ -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? {
Expand All @@ -357,5 +357,11 @@ public class ReadMoreTextView: UITextView {
}
return nil
}


}

extension String {
var length: Int {
return utf16.count
}
}
2 changes: 1 addition & 1 deletion Sources/UITextView+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 46f8e0a

Please sign in to comment.