Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

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

Abierto
#1,776 0 comentarios 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
35/100
Tipo de issue
Nueva funcionalidad
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
swift
Área
mobile

Línea de trabajo

Comienza con los puntos de entrada enabledDetectors, detectorAttributes y MessageKind.attributedText mostrados en el issue, y luego sigue cómo las coincidencias de los detectores se convierten en enlaces que se pueden tocar y llegan a didSelectUrl. Verifica el comportamiento con el texto atribuido personalizado super link 🌼 y determina qué debe permanecer intacto para que el título mostrado siga siendo interactivo. Se considera terminado cuando las URL detectadas muestran el título personalizado y siguen invocando didSelectUrl.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

question

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?

Lenguaje dominante
Swift
Estrellas
6.3k
Forks
1.2k
Merge medio
12 h 4 min
PR fusionados (30 d)
9

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de MessageKit/MessageKit

Todos los issues de MessageKit/MessageKit

Issues similares

Más issues de Swift

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.