Skip to content

Commit

Permalink
Introduce DetectionContext
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Sharanda authored and Pavel Sharanda committed Jun 5, 2023
1 parent 30d864f commit 61208cd
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 138 deletions.
31 changes: 25 additions & 6 deletions Demo/Sources/AttributedLabelDemoViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,52 @@ import UIKit
typealias TableViewCellStyle = UITableViewCell.CellStyle

extension String {
func styleAsTweet() -> AttributedStringBuilder {
func styleAsTweet() -> NSAttributedString {
let baseLinkAttrs = Attrs().foregroundColor(.blue)

let a = TagTuner {
Attrs(baseLinkAttrs).akaLink($0.tag.attributes["href"] ?? "")
}

let hashtag = DetectionTuner {
Attrs(baseLinkAttrs).akaLink("https://twitter.com/hashtag/\($0.text.replacingOccurrences(of: "#", with: ""))")
// ignore detection if akaLink was set for its range beforehand
if $0.firstExistingAttributeValue(for: .akaLink) != nil {
return Attrs()
} else {
return Attrs(baseLinkAttrs).akaLink("https://twitter.com/hashtag/\($0.text.replacingOccurrences(of: "#", with: ""))")
}
}

let mention = DetectionTuner {
Attrs(baseLinkAttrs).akaLink("https://twitter.com/\($0.text.replacingOccurrences(of: "@", with: ""))")
// ignore detection if akaLink was set for its range beforehand
if $0.firstExistingAttributeValue(for: .akaLink) != nil {
return Attrs()
} else {
return Attrs(baseLinkAttrs).akaLink("https://twitter.com/\($0.text.replacingOccurrences(of: "@", with: ""))")
}
}

let link = DetectionTuner {
Attrs(baseLinkAttrs).akaLink($0.text)
// ignore detection if akaLink was set for its range beforehand
if $0.firstExistingAttributeValue(for: .akaLink) != nil {
return Attrs()
} else {
return Attrs(baseLinkAttrs).akaLink($0.text)
}
}

return style(tags: ["a": a])
.styleHashtags(hashtag)
.styleMentions(mention)
.styleLinks(link)
.attributedString
}
}

var tweets: [String] = [
"Test <a href=\"https://www.muppets.com\">muppets.com</a> and www.kermit.com",
"Test <a href=\"https://instagram.com/hashtag/hashtag\">#hashtag</a> and #hashtag",
"Teat <a href=\"https://instagram.com/mention\">@mention</a> and @mention",
"Thank you for everything. My last ask is the same",
"@e2F If only Bradley's arm was longer. Best photo ever. 😊 #oscars https://pic.twitter.com/C9U5NOtGap<br>Check this <a href=\"https://github.com/psharanda/Atributika\">link</a>",
"@e2F If only Bradley's arm was longer. Best photo ever. 😊 #oscars😊 https://pic.twitter.com/C9U5NOtGap<br>Check this <a href=\"https://github.com/psharanda/Atributika\">link that won't detect click here If only Bradley's arm was longer. Best photo ever</a>",
Expand Down Expand Up @@ -180,7 +199,7 @@ class TweetCell: UITableViewCell {
return
}

tweetLabel.attributedText = tweet.styleAsTweet().attributedString
tweetLabel.attributedText = tweet.styleAsTweet()
}
}
}
Expand All @@ -194,7 +213,7 @@ class AttributedLabelDemoDetailsViewController: UIViewController {
return
}

attributedTextView.attributedText = tweet.styleAsTweet().attributedString
attributedTextView.attributedText = tweet.styleAsTweet()
}
}

Expand Down
Loading

0 comments on commit 61208cd

Please sign in to comment.