diff --git a/AttributedTextView.podspec b/AttributedTextView.podspec index 3f5a07f..310684c 100644 --- a/AttributedTextView.podspec +++ b/AttributedTextView.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'AttributedTextView' - s.version = '0.9.1' + s.version = '0.9.2' s.license = { :type => "MIT", :file => "LICENSE" } s.summary = 'Easiest way to create an attributed UILabel or UITextView (with support for multiple links and HTML)' s.homepage = 'https://github.com/evermeer/AttributedTextView' diff --git a/AttributedTextView.xcodeproj/project.pbxproj b/AttributedTextView.xcodeproj/project.pbxproj index f5d1a11..5ca27c0 100644 --- a/AttributedTextView.xcodeproj/project.pbxproj +++ b/AttributedTextView.xcodeproj/project.pbxproj @@ -41,6 +41,7 @@ 7FA506E61E695CEB003D83E7 /* AttributedLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7FA506E51E695CEB003D83E7 /* AttributedLabel.swift */; }; 7FA506E81E696EA2003D83E7 /* AttributedTextViewSubclassDemo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7FA506E71E696EA2003D83E7 /* AttributedTextViewSubclassDemo.swift */; }; 7FA506EA1E696EBD003D83E7 /* AttributedLabelSubclassDemo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7FA506E91E696EBD003D83E7 /* AttributedLabelSubclassDemo.swift */; }; + 7FA64F2E20D7DA7200E9345A /* String+NSRange.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7FA64F2D20D7DA7200E9345A /* String+NSRange.swift */; }; 7FD407B01E0C280E00AEBF1B /* Sample5.png in Resources */ = {isa = PBXBuildFile; fileRef = 7FD407AF1E0C280E00AEBF1B /* Sample5.png */; }; /* End PBXBuildFile section */ @@ -71,6 +72,7 @@ 7FA506E51E695CEB003D83E7 /* AttributedLabel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AttributedLabel.swift; sourceTree = ""; }; 7FA506E71E696EA2003D83E7 /* AttributedTextViewSubclassDemo.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AttributedTextViewSubclassDemo.swift; sourceTree = ""; }; 7FA506E91E696EBD003D83E7 /* AttributedLabelSubclassDemo.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AttributedLabelSubclassDemo.swift; sourceTree = ""; }; + 7FA64F2D20D7DA7200E9345A /* String+NSRange.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+NSRange.swift"; sourceTree = ""; }; 7FD407AF1E0C280E00AEBF1B /* Sample5.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Sample5.png; sourceTree = ""; }; 7FD880471DEF968500D299E1 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; }; 7FDAE1351DEF192A00CB46B6 /* Sample1.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Sample1.png; sourceTree = ""; }; @@ -170,6 +172,7 @@ 7FA506E51E695CEB003D83E7 /* AttributedLabel.swift */, 1304F89F1F86096800347B42 /* CGFloatDP.swift */, 7F198B63205035240009ACF3 /* NSAttributedString+Html.swift */, + 7FA64F2D20D7DA7200E9345A /* String+NSRange.swift */, ); name = Core; sourceTree = ""; @@ -455,6 +458,7 @@ 7F1C64871DEC722F002A1A59 /* AttributedTextView.swift in Sources */, 1304F8A01F86096800347B42 /* CGFloatDP.swift in Sources */, 7F198B64205035240009ACF3 /* NSAttributedString+Html.swift in Sources */, + 7FA64F2E20D7DA7200E9345A /* String+NSRange.swift in Sources */, 7F1C64791DEC722F002A1A59 /* Attributer.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/Sources/Attributer.swift b/Sources/Attributer.swift index 7adaa8b..b6cc030 100644 --- a/Sources/Attributer.swift +++ b/Sources/Attributer.swift @@ -527,8 +527,7 @@ open class Attributer { */ open func makeInteract(_ callback: @escaping ((_ link: String) -> ())) -> Attributer { for nsRange in self.ranges { - let iRange = self.attributedText.string.range(from: nsRange) - let unEscapedString = self.attributedText.string.substring(with: iRange!) + let unEscapedString = (self.attributedText.string as NSString).substring(with: nsRange) let escapedString = unEscapedString.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlHostAllowed) ?? "" self.attributedText.addAttribute(NSAttributedStringKey.link, value: "AttributedTextView:\(escapedString)", range: nsRange) urlCallbacks[unEscapedString] = callback diff --git a/Sources/String+NSRange.swift b/Sources/String+NSRange.swift new file mode 100644 index 0000000..f0ae37c --- /dev/null +++ b/Sources/String+NSRange.swift @@ -0,0 +1,16 @@ +// +// String+NSRange.swift +// AttributedTextView-iOS +// +// Created by Edwin Vermeer on 18-06-18. +// Copyright © 2018 evermeer. All rights reserved. +// + +import Foundation + +public extension String { + func substring(with nsrange: NSRange) -> Substring? { + guard let range = Range(nsrange, in: self) else { return nil } + return self[range] + } +}