Actualmente estoy usando una UIWebView para estilizar las publicaciones de Twitter. Algunos tweets, por supuesto, contienen URL, pero no contienen las etiquetas <a>
. Puedo extraer la URL, sin embargo, no estoy seguro de cómo agregar las etiquetas <a>
y volver a colocarlas en el tweet. A continuación, utilizaré el mismo enfoque aquí para agregar enlaces a los @usernames y #hashtags. He aquí un ejemplo de mi código actual:NSRegularExpression: Reemplazar el texto de la url con <a> etiquetas
NSString *tweet = @"Sync your files to your Google Docs with a folder on your desktop. Like Dropbox. Good choice, Google storage is cheap. http://ow.ly/4OaOo";
NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:@"(?i)\\b((?:[a-z][\\w-]+:(?:/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>?«»“”‘’]))" options:NSRegularExpressionCaseInsensitive error:NULL];
NSString *match = [tweet substringWithRange:[expression rangeOfFirstMatchInString:tweet options:NSMatchingCompleted range:NSMakeRange(0, [tweet length])]];
NSLog(@"%@", match);// == http://ow.ly/4OaOo
En última instancia, me gustaría que la cadena final a tener este aspecto:
Sync your files to your Google Docs with a folder on your desktop. Like Dropbox. Good choice, Google storage is cheap. <a href="http://ow.ly/4OaOo>http://ow.ly/4OaOo</a>
Cualquier ayuda sería muy apreciada.
Esto funcionará perfecto. Nunca pensé en usar Javascript. –
Puede ser lento con js, y también mi solución obj-c. –
En este caso, si usamos la expresión regular dos veces, se agrega el mismo enlace link se puede agregar otra –