Skip to content
This repository has been archived by the owner on Apr 18, 2023. It is now read-only.

Commit

Permalink
fix: update to link (#192)
Browse files Browse the repository at this point in the history
* update to link

* Update RichTextParser.swift

* Update RichTextParserSpec.swift

* Update RichTextParserSpec.swift

* Update RichTextParserSpec.swift
  • Loading branch information
BisuGit committed Jun 2, 2021
1 parent 74b3756 commit 025071f
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: fd6952a9b41a9a41bb7e6fb036901d60f1134d46

COCOAPODS: 1.9.1
COCOAPODS: 1.10.1
7 changes: 2 additions & 5 deletions Source/Text Parsing/RichTextParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -398,11 +398,8 @@ class RichTextParser {
let interactiveElementTagName = ParserConstants.interactiveElementTagName
let interactiveElementID = input.string.getSubstring(inBetween: "[\(interactiveElementTagName) id=", and: "]") ?? input.string
let interactiveElementText = input.string.getSubstring(inBetween: "]", and: "[/\(interactiveElementTagName)]") ?? input.string
let attributes: [NSAttributedString.Key: Any] = [
.customLink: interactiveElementID,
.foregroundColor: self.interactiveTextColor,
.font: self.font
].merging(input.attributes(at: 0, effectiveRange: nil)) { (current, _) in current }
let attributes: [NSAttributedString.Key: Any] = [.link: interactiveElementID].merging(input.attributes(at: 0, effectiveRange: nil)) { (current, _) in current
}
let mutableAttributedInput = NSMutableAttributedString(string: interactiveElementText, attributes: attributes)
return mutableAttributedInput
}
Expand Down
23 changes: 0 additions & 23 deletions Source/View Generators/UITextViewGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,33 +53,10 @@ class UITextViewGenerator {
textView.isScrollEnabled = false
textView.textContainerInset = .zero
textView.textContainer.lineFragmentPadding = 0
textView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(UITextViewGenerator.handleCustomLinkTapOnTextViewIfNecessary(_:))))
if #available(iOS 10.0, *) {
textView.adjustsFontForContentSizeCategory = true
}
textView.delegate = textViewDelegate
return textView
}

@objc static func handleCustomLinkTapOnTextViewIfNecessary(_ recognizer: UITapGestureRecognizer) {
guard let textView = recognizer.view as? UITextView else {
return
}

var location = recognizer.location(in: textView)
location.x -= textView.textContainerInset.left
location.y -= textView.textContainerInset.top
let tappedCharacterIndex = textView.layoutManager.characterIndex(
for: location,
in: textView.textContainer,
fractionOfDistanceBetweenInsertionPoints: nil
)
guard tappedCharacterIndex < textView.textStorage.length else {
return
}
if let linkID = textView.attributedText?.attribute(.customLink, at: tappedCharacterIndex, effectiveRange: nil) as? String,
let richTextViewDelegate = textView.delegate as? RichTextViewDelegate {
richTextViewDelegate.didTapCustomLink(withID: linkID)
}
}
}
8 changes: 2 additions & 6 deletions UnitTests/Text Parsing/RichTextParserSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,15 @@ class RichTextParserSpec: QuickSpec {
it("succesfully returns an NSAttributedString with the custom link property from a basic interactive element") {
let output = self.richTextParser.extractInteractiveElement(from: NSAttributedString(string: MarkDownText.basicInteractiveElement))
let attributes: [NSAttributedString.Key: Any] = [
.customLink: "123",
.foregroundColor: UIColor.blue,
.font: UIFont.systemFont(ofSize: UIFont.systemFontSize)
.link: "123",
]
let expectedAttributedString = NSAttributedString(string: "This is an interactive element", attributes: attributes)
expect(output).to(equal(expectedAttributedString))
}
it("succesfully returns an NSAttributedString with the custom link property from a complex interactive element") {
let output = self.richTextParser.extractInteractiveElement(from: NSAttributedString(string: MarkDownText.complexInteractiveElement))
let attributes: [NSAttributedString.Key: Any] = [
.customLink: "123",
.foregroundColor: UIColor.blue,
.font: UIFont.systemFont(ofSize: UIFont.systemFontSize)
.link: "123",
]
let expectedAttributedString = NSAttributedString(string: "element", attributes: attributes)
expect(output).to(equal(expectedAttributedString))
Expand Down

0 comments on commit 025071f

Please sign in to comment.