Skip to content

Commit

Permalink
Merge pull request optonaut#7 from superhuman/handleAttributed
Browse files Browse the repository at this point in the history
Adding support for attributed strings
  • Loading branch information
plivesey committed Mar 9, 2018
2 parents 8662183 + 8424c4b commit 1e8d498
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
15 changes: 12 additions & 3 deletions ActiveLabel/ActiveLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -282,18 +282,27 @@ typealias ElementTuple = (range: NSRange, element: ActiveElement, type: ActiveTy
return
}

let mutAttrString = addLineBreak(attributedText)
let mutAttrString = NSMutableAttributedString(attributedString: attributedText)

// If the string changes in length, then we need to reset the string (because the attributes have changed indexes)
// Otherwise, we can use the attributed string as is
var removeAttributes = false

if parseText {
clearActiveElements()
let newString = parseTextAndExtractActiveElements(mutAttrString)
mutAttrString.mutableString.setString(newString)
if newString.count != mutAttrString.string.count {
mutAttrString.mutableString.setString(newString)
removeAttributes = true
}
}

addLinkAttribute(mutAttrString)
textStorage.setAttributedString(mutAttrString)
_customizing = true
text = mutAttrString.string
if removeAttributes {
text = mutAttrString.string
}
_customizing = false
setNeedsDisplay()
}
Expand Down
17 changes: 9 additions & 8 deletions ActiveLabelTests/ActiveTypeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -255,23 +255,23 @@ class ActiveTypeTests: XCTestCase {
}

func testRemoveHandleMention() {
label.handleMentionTap({_ in })
label.handleMentionTap({_, _ in })
XCTAssertNotNil(label.handleMentionTap)

label.removeHandle(for: .mention)
XCTAssertNil(label.mentionTapHandler)
}

func testRemoveHandleHashtag() {
label.handleHashtagTap({_ in })
label.handleHashtagTap({_, _ in })
XCTAssertNotNil(label.handleHashtagTap)

label.removeHandle(for: .hashtag)
XCTAssertNil(label.hashtagTapHandler)
}

func testRemoveHandleURL() {
label.handleURLTap({_ in })
label.handleURLTap({_, _ in })
XCTAssertNotNil(label.handleURLTap)

label.removeHandle(for: .url)
Expand All @@ -282,8 +282,8 @@ class ActiveTypeTests: XCTestCase {
let newType1 = ActiveType.custom(pattern: "\\sare1\\b")
let newType2 = ActiveType.custom(pattern: "\\sare2\\b")

label.handleCustomTap(for: newType1, handler: {_ in })
label.handleCustomTap(for: newType2, handler: {_ in })
label.handleCustomTap(for: newType1, handler: {_, _ in })
label.handleCustomTap(for: newType2, handler: {_, _ in })
XCTAssertEqual(label.customTapHandlers.count, 2)

label.removeHandle(for: newType1)
Expand Down Expand Up @@ -397,12 +397,13 @@ class ActiveTypeTests: XCTestCase {
XCTAssertEqual(currentElementType, customEmptyType)
}

// urlMaxiumumLength no longer works because we never want to change the string length now that we've enabled attributed strings
func testStringTrimming() {
let text = "Tweet with long url: https://twitter.com/twicket_app/status/649678392372121601 and short url: https://hello.co"
label.urlMaximumLength = 30
label.text = text

XCTAssertNotEqual(text.characters.count, label.text!.characters.count)
XCTAssertNotEqual(text.count, label.text!.count)
}

func testStringTrimmingURLShorterThanLimit() {
Expand All @@ -413,6 +414,7 @@ class ActiveTypeTests: XCTestCase {
XCTAssertEqual(text, label.text!)
}

// urlMaxiumumLength no longer works because we never want to change the string length now that we've enabled attributed strings
func testStringTrimmingURLLongerThanLimit() {
let trimLimit = 30
let url = "https://twitter.com/twicket_app/status/649678392372121601"
Expand All @@ -422,7 +424,7 @@ class ActiveTypeTests: XCTestCase {
label.text = text


XCTAssertNotEqual(text.characters.count, label.text!.characters.count)
XCTAssertNotEqual(text.count, label.text!.count)

switch activeElements.first! {
case .url(let original, let trimmed):
Expand All @@ -431,6 +433,5 @@ class ActiveTypeTests: XCTestCase {
default:
XCTAssert(false)
}

}
}

0 comments on commit 1e8d498

Please sign in to comment.