From 0ef8b1d5224d36a28362f4f44fd8d3e9dd9099d0 Mon Sep 17 00:00:00 2001 From: Pavel Sharanda Date: Mon, 9 Oct 2017 11:55:39 +0300 Subject: [PATCH] Emojis display bug fix, migration to swift 4 Strings API --- Demo/Snippet.swift | 4 ++-- Demo/SnippetsViewController.swift | 2 -- ...TTTAttributedLabelDemoViewController.swift | 9 +++----- README.md | 12 ++++------ Sources/Atributika.swift | 8 +++---- Sources/String+Detection.swift | 22 +++++++++---------- Tests/AtributikaTests/AtributikaTests.swift | 17 ++++++++++++-- 7 files changed, 39 insertions(+), 35 deletions(-) diff --git a/Demo/Snippet.swift b/Demo/Snippet.swift index 8886fa1..6184889 100644 --- a/Demo/Snippet.swift +++ b/Demo/Snippet.swift @@ -24,7 +24,7 @@ func test0() -> NSAttributedString { func test1() -> NSAttributedString { let b = Style("b").font(.boldSystemFont(ofSize: 20)).foregroundColor(.red) - let str = "Hello World!!!".style(tags: b) + let str = "Hello W🌎rld!!!".style(tags: b) .styleAll(Style.font(.systemFont(ofSize: 20))) .attributedString return str @@ -66,7 +66,7 @@ func test4() -> NSAttributedString { func test5() -> NSAttributedString { - let str = "firstsecondthird".style(tags: + let str = "firstsec⚽️ndthird".style(tags: Style("r").foregroundColor(.red), Style("g").foregroundColor(.green), Style("b").foregroundColor(.blue)).attributedString diff --git a/Demo/SnippetsViewController.swift b/Demo/SnippetsViewController.swift index 13510e8..cff3e1e 100644 --- a/Demo/SnippetsViewController.swift +++ b/Demo/SnippetsViewController.swift @@ -30,8 +30,6 @@ class SnippetsViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() - - view.addSubview(tableView) } diff --git a/Demo/TTTAttributedLabelDemoViewController.swift b/Demo/TTTAttributedLabelDemoViewController.swift index 8957ca9..c6b7131 100644 --- a/Demo/TTTAttributedLabelDemoViewController.swift +++ b/Demo/TTTAttributedLabelDemoViewController.swift @@ -43,14 +43,11 @@ class TTTAttributedLabelDemoViewController: UIViewController { aka.detections.forEach { detection in switch detection.type { case .hashtag: - let startIndex = aka.string.index(aka.string.startIndex, offsetBy: detection.range.lowerBound + 1) - let endIndex = aka.string.index(aka.string.startIndex, offsetBy: detection.range.lowerBound + detection.range.count - 1) - - let hashtag = (aka.string[startIndex...endIndex]) - label.addLink(to: URL(string: "https://twitter.com/hashtag/\(hashtag)"), with: NSRange(detection.range)) + let hashtag = (aka.string[detection.range]) + label.addLink(to: URL(string: "https://twitter.com/hashtag/\(hashtag)"), with: NSRange(detection.range, in: aka.string)) case .tag(let tag): if tag.name == "a", let href = tag.attributes["href"] { - label.addLink(to: URL(string: href), with: NSRange(detection.range)) + label.addLink(to: URL(string: href), with: NSRange(detection.range, in: aka.string)) } default: break diff --git a/README.md b/README.md index 7ad63e7..5ba7edf 100644 --- a/README.md +++ b/README.md @@ -128,15 +128,11 @@ let aka = "If only Bradley's arm was longer. Best photo ever. #oscars + public let range: Range } public protocol AtributikaProtocol { @@ -52,7 +52,7 @@ extension AtributikaProtocol { for d in detections { if d.style.attributes.count > 0 { - attributedString.addAttributes(d.style.attributes, range: NSRange(d.range)) + attributedString.addAttributes(d.style.attributes, range: NSRange(d.range, in: string)) } } @@ -105,7 +105,7 @@ extension AtributikaProtocol { return Atributika(string: string, detections: detections + ds, baseStyle: baseStyle) } - public func style(range: Range, style: Style) -> Atributika { + public func style(range: Range, style: Style) -> Atributika { let d = Detection(type: .none, style: style, range: range) return Atributika(string: string, detections: detections + [d], baseStyle: baseStyle) } @@ -154,7 +154,7 @@ extension NSAttributedString: AtributikaProtocol { var ds: [Detection] = [] enumerateAttributes(in: NSMakeRange(0, length), options: []) { (attributes, range, _) in - if let range = Range(range) { + if let range = Range(range, in: self.string) { ds.append(Detection(type: .none, style: Style("", attributes), range: range)) } } diff --git a/Sources/String+Detection.swift b/Sources/String+Detection.swift index fc87f23..7d59641 100644 --- a/Sources/String+Detection.swift +++ b/Sources/String+Detection.swift @@ -15,7 +15,7 @@ public struct Tag { public struct TagInfo { public let tag: Tag - public let range: Range + public let range: Range } extension String { @@ -70,7 +70,7 @@ extension String { scanner.charactersToBeSkipped = nil var resultString = String() var tagsResult = [TagInfo]() - var tagsStack = [(Tag, Int)]() + var tagsStack = [(Tag, String.Index)]() while !scanner.isAtEnd { @@ -86,7 +86,7 @@ extension String { if tag.name == "br" { resultString += "\n" } else { - let resultTextEndIndex = resultString.characters.count + let resultTextEndIndex = resultString.endIndex if open { tagsStack.append((tag, resultTextEndIndex)) @@ -117,23 +117,23 @@ extension String { return (resultString, tagsResult) } - public func detectHashTags() -> [Range] { + public func detectHashTags() -> [Range] { return detect(regex: "[#]\\w\\S*\\b") } - public func detectMentions() -> [Range] { + public func detectMentions() -> [Range] { return detect(regex: "[@]\\w\\S*\\b") } - public func detect(regex: String, options: NSRegularExpression.Options = []) -> [Range] { + public func detect(regex: String, options: NSRegularExpression.Options = []) -> [Range] { - var ranges = [Range]() + var ranges = [Range]() let dataDetector = try? NSRegularExpression(pattern: regex, options: options) dataDetector?.enumerateMatches(in: self, options: [], range: NSMakeRange(0, (self as NSString).length), using: { (result, flags, _) in - if let r = result, let range = Range(r.range) { + if let r = result, let range = Range(r.range, in: self) { ranges.append(range) } }) @@ -141,13 +141,13 @@ extension String { return ranges } - public func detect(textCheckingTypes: NSTextCheckingTypes) -> [Range] { + public func detect(textCheckingTypes: NSTextCheckingTypes) -> [Range] { - var ranges = [Range]() + var ranges = [Range]() let dataDetector = try? NSDataDetector(types: textCheckingTypes) dataDetector?.enumerateMatches(in: self, options: [], range: NSMakeRange(0, (self as NSString).length), using: { (result, flags, _) in - if let r = result, let range = Range(r.range) { + if let r = result, let range = Range(r.range, in: self) { ranges.append(range) } }) diff --git a/Tests/AtributikaTests/AtributikaTests.swift b/Tests/AtributikaTests/AtributikaTests.swift index 05d440c..72d7074 100644 --- a/Tests/AtributikaTests/AtributikaTests.swift +++ b/Tests/AtributikaTests/AtributikaTests.swift @@ -95,7 +95,7 @@ class AtributikaTests: XCTestCase { XCTAssertEqual(a.attributedString, reference) - XCTAssertEqual(a.detections[0].range, 0..<5) + XCTAssertEqual(a.detections[0].range, a.string.startIndex..