Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to replace detected link with custom title for that link? #1776

Open
kunass2 opened this issue Feb 13, 2023 · 0 comments
Open

How to replace detected link with custom title for that link? #1776

kunass2 opened this issue Feb 13, 2023 · 0 comments
Labels

Comments

@kunass2
Copy link

kunass2 commented Feb 13, 2023

These are my delegate methods for my chat:

    func enabledDetectors(for _: MessageType, at _: IndexPath, in _: MessagesCollectionView) -> [DetectorType] {
        [.url, .phoneNumber]
    }

    func detectorAttributes(
        for _: DetectorType,
        and message: MessageType,
        at _: IndexPath
    ) -> [NSAttributedString.Key: Any] {
        [.underlineStyle: 1, .foregroundColor: isFromCurrentSender(message: message) ? UIColor.white : UIColor.black]
    }

And this is how it looks now:

enter image description here

I have tried to replace attributedString for messageKind like this:

    var kind: MessageKind {
        return .attributedText(attributedString.prettyLinked)
    }

where:

    extension NSAttributedString {
        var prettyLinked: NSAttributedString {
            let mutableString = NSMutableAttributedString(attributedString: self)
            do {
                let detector = try NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue)
                let fullRange = NSRange(location: 0, length: string.utf16.count)
                let matches = detector.matches(in: string, range: fullRange)
                matches.reversed().forEach { match in
                    let range = match.range
                    let link = (string as NSString).substring(with: range)
                    let prettyLink = NSAttributedString(string: "super link 🌼", attributes: [.link: link])
                    mutableString.replaceCharacters(in: range, with: prettyLink)
                }
            } catch {}
            return NSAttributedString(attributedString: mutableString)
        }
    }

The result is like:

enter image description here

But it is not tappable, after that change. Why?

What do I want to achieve is to replace that urls with any string, for example here is super link 🌼. And then didSelectUrl is called as always. Is it possible with MessageKit?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant