Fix refactoring bug and add MessageLabelDelegate property

This commit is contained in:
Steven Deutsch
2017-08-23 23:50:52 -05:00
parent 035afbdcb6
commit ba85191dd0
2 changed files with 20 additions and 8 deletions
+19 -8
View File
@@ -53,7 +53,7 @@ open class MessageLabel: UILabel, UIGestureRecognizerDelegate {
// MARK: - Public Properties
open var delegate: MessageLabelDelegate?
open weak var delegate: MessageLabelDelegate?
open var enabledDetectors: [DetectorType] = [.phoneNumber, .address, .date, .url]
@@ -219,7 +219,15 @@ open class MessageLabel: UILabel, UIGestureRecognizerDelegate {
return
}
let textWithDetectorAttributes = addDetectorAttributes(to: attributedText)
guard let checkingResults = parse(text: attributedText, for: enabledDetectors), checkingResults.isEmpty == false else {
textStorage.setAttributedString(attributedText)
setNeedsDisplay()
return
}
setRangesForDetectors(in: checkingResults)
let textWithDetectorAttributes = addDetectorAttributes(to: attributedText, for: checkingResults)
textStorage.setAttributedString(textWithDetectorAttributes)
@@ -227,14 +235,10 @@ open class MessageLabel: UILabel, UIGestureRecognizerDelegate {
}
private func addDetectorAttributes(to text: NSAttributedString) -> NSAttributedString {
private func addDetectorAttributes(to text: NSAttributedString, for checkingResults: [NSTextCheckingResult]) -> NSAttributedString {
let mutableAttributedString = NSMutableAttributedString(attributedString: text)
guard enabledDetectors.isEmpty == false else { return mutableAttributedString }
guard let checkingResults = parse(text: text, for: enabledDetectors) else { return mutableAttributedString }
guard checkingResults.isEmpty == false else { return mutableAttributedString }
checkingResults.forEach { result in
let attributes = detectorAttributes(for: result.resultType)
mutableAttributedString.addAttributes(attributes, range: result.range)
@@ -292,7 +296,7 @@ open class MessageLabel: UILabel, UIGestureRecognizerDelegate {
// MARK: - Parsing Text
private func parse(text: NSAttributedString, for detectorTypes: [DetectorType]) -> [NSTextCheckingResult]? {
guard detectorTypes.isEmpty == false else { return nil }
let checkingTypes = detectorTypes.reduce(0) { $0 | $1.textCheckingType.rawValue }
let detector = try? NSDataDetector(types: checkingTypes)
@@ -353,6 +357,8 @@ open class MessageLabel: UILabel, UIGestureRecognizerDelegate {
private func setupGestureRecognizers() {
print("Setting up gesture recognizers")
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleGesture(_:)))
addGestureRecognizer(tapGesture)
tapGesture.delegate = self
@@ -366,6 +372,8 @@ open class MessageLabel: UILabel, UIGestureRecognizerDelegate {
func handleGesture(_ gesture: UIGestureRecognizer) {
print("Handling gesture")
let touchLocation = gesture.location(ofTouch: 0, in: self)
guard let index = stringIndex(at: touchLocation) else { return }
@@ -382,6 +390,8 @@ open class MessageLabel: UILabel, UIGestureRecognizerDelegate {
}
private func handleGesture(for detectorType: DetectorType, text: NSAttributedString) {
print("Detector routing")
switch detectorType {
case .address:
handleAddress(address: text)
@@ -395,6 +405,7 @@ open class MessageLabel: UILabel, UIGestureRecognizerDelegate {
}
private func handleAddress(address: NSAttributedString) {
print("Ok")
delegate?.didSelectAddress(address.string)
}
+1
View File
@@ -166,6 +166,7 @@ extension MessagesViewController: UICollectionViewDataSource {
if let messageLabelDelegate = messagesCollectionView.messageLabelDelegate {
cell.messageLabel.delegate = messageLabelDelegate
print("Setting delegate")
}
guard let displayDataSource = messagesDataSource as? MessagesDisplayDataSource else { return cell }