Skip to content

Commit

Permalink
Emojis display bug fix, migration to swift 4 Strings API
Browse files Browse the repository at this point in the history
  • Loading branch information
psharanda committed Oct 9, 2017
1 parent 7d7cc85 commit 0ef8b1d
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 35 deletions.
4 changes: 2 additions & 2 deletions Demo/Snippet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func test0() -> NSAttributedString {

func test1() -> NSAttributedString {
let b = Style("b").font(.boldSystemFont(ofSize: 20)).foregroundColor(.red)
let str = "Hello <b>World</b>!!!".style(tags: b)
let str = "Hello <b>W🌎rld</b>!!!".style(tags: b)
.styleAll(Style.font(.systemFont(ofSize: 20)))
.attributedString
return str
Expand Down Expand Up @@ -66,7 +66,7 @@ func test4() -> NSAttributedString {

func test5() -> NSAttributedString {

let str = "<r>first</r><g>second</g><b>third</b>".style(tags:
let str = "<r>first</r><g>sec⚽️nd</g><b>third</b>".style(tags:
Style("r").foregroundColor(.red),
Style("g").foregroundColor(.green),
Style("b").foregroundColor(.blue)).attributedString
Expand Down
2 changes: 0 additions & 2 deletions Demo/SnippetsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ class SnippetsViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()


view.addSubview(tableView)
}

Expand Down
9 changes: 3 additions & 6 deletions Demo/TTTAttributedLabelDemoViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 4 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,11 @@ let aka = "If only Bradley's arm was longer. Best photo ever. #oscars <a href=\"
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
Expand All @@ -159,7 +155,7 @@ extension XXX: TTTAttributedLabelDelegate {

Current version is compatible with:

* Swift 3.0+
* Swift 4.0+
* iOS 8.0 or later
* tvOS 9.0 or later
* watchOS 2.0 or later
Expand Down
8 changes: 4 additions & 4 deletions Sources/Atributika.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public enum DetectionType {
public struct Detection {
public let type: DetectionType
public let style: Style
public let range: Range<Int>
public let range: Range<String.Index>
}

public protocol AtributikaProtocol {
Expand All @@ -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))
}
}

Expand Down Expand Up @@ -105,7 +105,7 @@ extension AtributikaProtocol {
return Atributika(string: string, detections: detections + ds, baseStyle: baseStyle)
}

public func style(range: Range<Int>, style: Style) -> Atributika {
public func style(range: Range<String.Index>, style: Style) -> Atributika {
let d = Detection(type: .none, style: style, range: range)
return Atributika(string: string, detections: detections + [d], baseStyle: baseStyle)
}
Expand Down Expand Up @@ -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))
}
}
Expand Down
22 changes: 11 additions & 11 deletions Sources/String+Detection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public struct Tag {

public struct TagInfo {
public let tag: Tag
public let range: Range<Int>
public let range: Range<String.Index>
}

extension String {
Expand Down Expand Up @@ -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 {

Expand All @@ -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))
Expand Down Expand Up @@ -117,37 +117,37 @@ extension String {
return (resultString, tagsResult)
}

public func detectHashTags() -> [Range<Int>] {
public func detectHashTags() -> [Range<String.Index>] {

return detect(regex: "[#]\\w\\S*\\b")
}

public func detectMentions() -> [Range<Int>] {
public func detectMentions() -> [Range<String.Index>] {

return detect(regex: "[@]\\w\\S*\\b")
}

public func detect(regex: String, options: NSRegularExpression.Options = []) -> [Range<Int>] {
public func detect(regex: String, options: NSRegularExpression.Options = []) -> [Range<String.Index>] {

var ranges = [Range<Int>]()
var ranges = [Range<String.Index>]()

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)
}
})

return ranges
}

public func detect(textCheckingTypes: NSTextCheckingTypes) -> [Range<Int>] {
public func detect(textCheckingTypes: NSTextCheckingTypes) -> [Range<String.Index>] {

var ranges = [Range<Int>]()
var ranges = [Range<String.Index>]()

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)
}
})
Expand Down
17 changes: 15 additions & 2 deletions Tests/AtributikaTests/AtributikaTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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..<a.string.index(a.string.startIndex, offsetBy: 5))

if case .tag(let tag) = a.detections[0].type {
XCTAssertEqual(tag.name, "a")
Expand Down Expand Up @@ -264,15 +264,28 @@ class AtributikaTests: XCTestCase {
}

func testRange() {
let str = "Hello World!!!"

let test = "Hello World!!!".style(range: 0..<5, style: Style("b").font(.boldSystemFont(ofSize: 45))).attributedString
let test = "Hello World!!!".style(range: str.startIndex..<str.index(str.startIndex, offsetBy: 5), style: Style("b").font(.boldSystemFont(ofSize: 45))).attributedString

let reference = NSMutableAttributedString(string: "Hello World!!!")
reference.addAttributes([NSAttributedStringKey.font: Font.boldSystemFont(ofSize: 45)], range: NSMakeRange(0, 5))

XCTAssertEqual(test, reference)
}

func testEmojis() {

let test = "Hello <b>W🌎rld</b>!!!".style(tags:
Style("b").font(.boldSystemFont(ofSize: 45))
).attributedString

let reference = NSMutableAttributedString(string: "Hello W🌎rld!!!")
reference.addAttributes([NSAttributedStringKey.font: Font.boldSystemFont(ofSize: 45)], range: NSMakeRange(6, 6))

XCTAssertEqual(test,reference)
}

}

#if os(Linux)
Expand Down

0 comments on commit 0ef8b1d

Please sign in to comment.