1

つぶやきを見つける機能を構築しようとしていますhashtags。そしてそれらを . で囲みますHTML <a> tag。それらにリンクできるようにします。これが私がすることです。

NSError* error = nil;

NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@"(?:\\s|\\A)[##]+([A-Za-z0-9-_]+)" options:0 error:&error];
NSArray* matches = [regex matchesInString:tweetText options:0 range:NSMakeRange(0, [tweetText length])];
for ( NSTextCheckingResult* match in matches )
{
    NSString* matchText = [tweetText substringWithRange:[match range]];
    NSString *matchText2 = [matchText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString *search = [matchText2 stringByReplacingOccurrencesOfString:@"#"
                                                            withString:@""];
    NSString *searchHTML= [NSString stringWithFormat:@"<a href='https://twitter.com/search?q=%%23%@'>%@</a>",search,matchText];
    tweetText = [tweetText stringByReplacingOccurrencesOfString:matchText
                                                    withString:searchHTML];
    NSLog(@"match: %@", tweetText);
}

この関数を実行する前に、tweetText を別の関数でループして URL を見つけます。そのため、ツイートには次の内容を含めることができます。<a href='http://google.be' target='_blank'>http://google.be</a>

現在、ハッシュタグだけでなく、他のリンクの周りに別のタグを配置することがあります。

誰かがこれを手伝ってくれますか。

ヒント

次のJAVAコードをOBJ-Cに変換しようとしています

  String patternStr = "(?:\\s|\\A)[##]+([A-Za-z0-9-_]+)"
     Pattern pattern = Pattern.compile(patternStr)
     Matcher matcher = pattern.matcher(tweetText)
     String result = "";

     // Search for Hashtags
     while (matcher.find()) {
     result = matcher.group();
     result = result.replace(" ", "");
     String search = result.replace("#", "");
     String searchHTML="<a href='http://search.twitter.com/search?q=" + search + "'>" + result + "</a>"
     tweetText = tweetText.replace(result,searchHTML);
     }

編集

Gers, we kijken er al naar uit! “@GersPardoel: We zitten in België straks naar Genk!!<a href='<a href<a href='https://twitter.com/search?q=%23='http'>='http</a>s://twitter.com/search?q=%23https:/'>https:/</a>/twitter.com/search?q=%23engaan'> #engaan</a>” #GOS12 #genk #fb
4

1 に答える 1