diff --git a/Atributika.podspec b/Atributika.podspec index b00cd4b..865a5a5 100644 --- a/Atributika.podspec +++ b/Atributika.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "Atributika" - s.version = "4.7.1" + s.version = "4.7.2" s.summary = "Convert text with HTML tags, hashtags, mentions, links into NSAttributedString. Make them clickable with UILabel drop-in replacement." s.description = <<-DESC `Atributika` is an easy and painless way to build NSAttributedString. It is able to detect HTML-like tags, links, phone numbers, hashtags, any regex or even standard ios data detectors and style them with various attributes like font, color, etc. `Atributika` comes with drop-in label replacement `AttributedLabel` which is able to make any detection clickable. diff --git a/Demo/AttributedLabelDemoViewController.swift b/Demo/AttributedLabelDemoViewController.swift index 9fdac8f..dee12c6 100644 --- a/Demo/AttributedLabelDemoViewController.swift +++ b/Demo/AttributedLabelDemoViewController.swift @@ -38,7 +38,9 @@ class AttributedLabelDemoViewController: UIViewController { "RT or tweet #camilahammersledge for a follow 👽", "Denny JA: Dengan RT ini, anda ikut memenangkan Jokowi-JK. Pilih pemimpin yg bisa dipercaya (Jokowi) dan pengalaman (JK). #DJoJK", "Always in my heart @Harry_Styles . Yours sincerely, Louis", - "HELP ME PLEASE. A MAN NEEDS HIS NUGGS https://pbs.twimg.com/media/C8sk8QlUwAAR3qI.jpg" + "HELP ME PLEASE. A MAN NEEDS HIS NUGGS https://pbs.twimg.com/media/C8sk8QlUwAAR3qI.jpg", + "Подтверждая номер телефона, вы\nпринимаете «пользовательское соглашение»", + "Here's how a similar one was solved 😄 \nhttps://medium.com/@narcelio/solving-decred-mockingbird-puzzle-5366efeaeed7\n" ] init() { diff --git a/Sources/AttributedLabel.swift b/Sources/AttributedLabel.swift index 9a7b115..418f4d7 100644 --- a/Sources/AttributedLabel.swift +++ b/Sources/AttributedLabel.swift @@ -222,7 +222,7 @@ open class AttributedLabel: UIView { if let detection = state.detection { let higlightedAttributedString = NSMutableAttributedString(attributedString: string) - higlightedAttributedString.addAttributes(detection.style.highlightedAttributes, range: NSRange(detection.range, in: string.string)) + higlightedAttributedString.addAttributes(detection.style.highlightedAttributes, range: NSRange(detection.range, in: text.string)) label.attributedText = higlightedAttributedString } else { if state.isEnabled { diff --git a/Sources/String+Detection.swift b/Sources/String+Detection.swift index 69179db..d49ec03 100644 --- a/Sources/String+Detection.swift +++ b/Sources/String+Detection.swift @@ -91,21 +91,28 @@ extension String { public func detectTags(transformers: [TagTransformer] = []) -> (string: String, tagsInfo: [TagInfo]) { + struct TagInfoInternal { + public let tag: Tag + public let rangeStart: Int + public let rangeEnd: Int + public let level: Int + } + let scanner = Scanner(string: self) scanner.charactersToBeSkipped = nil var resultString = String() - var tagsResult = [TagInfo]() - var tagsStack = [(Tag, String.Index, Int)]() + var tagsResult = [TagInfoInternal]() + var tagsStack = [(Tag, Int, Int)]() while !scanner.isAtEnd { if let textString = scanner.scanUpToCharacters(from: CharacterSet(charactersIn: "<&")) { - resultString += textString + resultString.append(textString) } else { if scanner.scanString("<") != nil { if scanner.isAtEnd { - resultString += "<" + resultString.append("<") } else { let nextChar = (scanner.string as NSString).substring(with: NSRange(location: scanner.scanLocation, length: 1)) if CharacterSet.letters.contains(nextChar.unicodeScalars.first!) || (nextChar == "/") { @@ -115,12 +122,12 @@ extension String { if scanner.scanString(">") != nil { if let tag = parseTag(tagString, parseAttributes: tagType == .start ) { - let resultTextEndIndex = resultString.endIndex + let resultTextEndIndex = resultString.count if let transformer = transformers.first(where: { $0.tagName.lowercased() == tag.name.lowercased() && $0.tagType == tagType }) { - resultString += transformer.transform(tag) + resultString.append(transformer.transform(tag)) } if tagType == .start { @@ -128,7 +135,7 @@ extension String { } else { for (index, (tagInStack, startIndex, level)) in tagsStack.enumerated().reversed() { if tagInStack.name.lowercased() == tag.name.lowercased() { - tagsResult.append(TagInfo(tag: tagInStack, range: startIndex.. [Range] {