Skip to content

Commit 9da450d

Browse files
authored
Merge pull request #1 from badoo/switched_to_compiler_check
Fix range calculation
2 parents 2818118 + 26006bb commit 9da450d

3 files changed

Lines changed: 38 additions & 16 deletions

File tree

HyperLabel/HyperLabelTextStyler.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ public struct HyperLabelTextStyler {
3030
at range: Range<String.Index>) -> NSAttributedString {
3131
guard !self.linkAttributes.isEmpty else { return attributedString }
3232
let mutable = attributedString.mutableCopy() as! NSMutableAttributedString
33-
let text = mutable.string
33+
// https://bugs.swift.org/browse/SR-11330
34+
let text = mutable.string + ""
3435
let range = NSRange(range, in: text)
3536
mutable.addAttributes(self.linkAttributes, range: range)
3637
return mutable.copy() as! NSAttributedString

HyperLabel/TextLayoutInfoProvider.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ final class TextLayoutInfoProvider {
5858
let usedRect = self.layoutManager.usedRect(for: self.textContainer)
5959
guard usedRect.contains(point) else { return nil }
6060
let index = self.layoutManager.glyphIndex(for: point, in: self.textContainer)
61-
#if swift(>=5)
61+
#if compiler(>=5)
6262
return String.Index(utf16Offset: index,
6363
in: self.textStorage.string)
6464
#else

HyperLabelTests/HyperLabelTests.swift

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,45 @@ import XCTest
2626

2727
class HyperLabelTests: XCTestCase {
2828

29-
override func setUp() {
30-
// Put setup code here. This method is called before the invocation of each test method in the class.
31-
}
29+
override func setUp() { }
3230

33-
override func tearDown() {
34-
// Put teardown code here. This method is called after the invocation of each test method in the class.
35-
}
31+
override func tearDown() { }
32+
33+
func test_GivenTextWithSpecialCharacter_WhenAddLink_ThenRangesAreCorrect() {
34+
// Given
35+
// '’' is UTF16?
36+
let exampleText = "You’ll be happy one day."
37+
let sut: HyperLabel = {
38+
let hyperLabel = HyperLabel()
39+
hyperLabel.numberOfLines = 0
40+
hyperLabel.text = exampleText
41+
hyperLabel.additionalLinkAttributes = [.underlineStyle: NSUnderlineStyle.single.rawValue]
42+
return hyperLabel
43+
}()
3644

37-
func testExample() {
38-
// This is an example of a functional test case.
39-
// Use XCTAssert and related functions to verify your tests produce the correct results.
45+
// When
46+
let range = exampleText.range(of: "day")!
47+
48+
// Then
49+
sut.addLink(withRange: range) {}
4050
}
4151

42-
func testPerformanceExample() {
43-
// This is an example of a performance test case.
44-
self.measure {
45-
// Put the code you want to measure the time of here.
46-
}
52+
func test_GivenSimpleText_WhenAddLink_ThenRangesAreCorrect() {
53+
// Given
54+
let exampleText = "Look's good with no special characters."
55+
let sut: HyperLabel = {
56+
let hyperLabel = HyperLabel()
57+
hyperLabel.numberOfLines = 0
58+
hyperLabel.text = exampleText
59+
hyperLabel.additionalLinkAttributes = [.underlineStyle: NSUnderlineStyle.single.rawValue]
60+
return hyperLabel
61+
}()
62+
63+
// When
64+
let range = exampleText.range(of: "characters")!
65+
66+
// Then
67+
sut.addLink(withRange: range) {}
4768
}
4869

4970
}

0 commit comments

Comments
 (0)