Skip to content

Commit

Permalink
solved range warning
Browse files Browse the repository at this point in the history
  • Loading branch information
evermeer committed Jun 18, 2018
1 parent 284714e commit 6bb2e19
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion AttributedTextView.podspec
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
4 changes: 4 additions & 0 deletions AttributedTextView.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down Expand Up @@ -71,6 +72,7 @@
7FA506E51E695CEB003D83E7 /* AttributedLabel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AttributedLabel.swift; sourceTree = "<group>"; };
7FA506E71E696EA2003D83E7 /* AttributedTextViewSubclassDemo.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AttributedTextViewSubclassDemo.swift; sourceTree = "<group>"; };
7FA506E91E696EBD003D83E7 /* AttributedLabelSubclassDemo.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AttributedLabelSubclassDemo.swift; sourceTree = "<group>"; };
7FA64F2D20D7DA7200E9345A /* String+NSRange.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+NSRange.swift"; sourceTree = "<group>"; };
7FD407AF1E0C280E00AEBF1B /* Sample5.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Sample5.png; sourceTree = "<group>"; };
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 = "<group>"; };
Expand Down Expand Up @@ -170,6 +172,7 @@
7FA506E51E695CEB003D83E7 /* AttributedLabel.swift */,
1304F89F1F86096800347B42 /* CGFloatDP.swift */,
7F198B63205035240009ACF3 /* NSAttributedString+Html.swift */,
7FA64F2D20D7DA7200E9345A /* String+NSRange.swift */,
);
name = Core;
sourceTree = "<group>";
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions Sources/Attributer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions Sources/String+NSRange.swift
Original file line number Diff line number Diff line change
@@ -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]
}
}

0 comments on commit 6bb2e19

Please sign in to comment.