2011-03-08 67 views
2

我正在開發一個推特應用,需要一些幫助。我想替換@usernames和URL的w /粗體藍色文本,並鏈接到我的應用程序中相應的視圖。我可以解析tweet文本,並使用@usernames和URL構建一個數組,但是我還沒有想出如何解析和替換內聯的@usernames和URL的w /風格化文本。解析並替換iphone應用中的@usernames和URL's

基本上我希望文本看起來與官方Twitter應用程序(又名Tweetie)上的tweet文本類似。

下面是我在google上發現的推文示例。您會注意到@ usernames和鏈接是藍色的: http://www.tipb.com/images/stories/2009/02/twitter_celebs_iphone.jpg

我正在使用來自Google Toolbox for Mac的GTMRegex正則表達式。

這裏是我當前的代碼:

- (void)parseTweet { 
NSString *text = message.text; 
NSArray *a; 

a = [text gtm_allSubstringsMatchedByPattern:@"@[[:alnum:]_]+"]; 
for (NSString *s in a) { 
    NTLNURLPair *pair = [[NTLNURLPair alloc] init]; 
    pair.text = [NSString stringWithFormat:@"@%@", [s substringFromIndex:1]]; 
    pair.screenName = s; 
    [links addObject:pair]; 
    [pair release]; 
} 
a = [text gtm_allSubstringsMatchedByPattern:@"http:\\/\\/[^[:space:]]+"]; 
for (NSString *s in a) { 
    NTLNURLPair *pair = [[NTLNURLPair alloc] init]; 
    pair.text = s; 
    pair.url = s; 
    [links addObject:pair]; 
    [pair release]; 
} 
a = [text gtm_allSubstringsMatchedByPattern:@"https:\\/\\/[^[:space:]]+"]; 
for (NSString *s in a) { 
    NTLNURLPair *pair = [[NTLNURLPair alloc] init]; 
    pair.text = s; 
    pair.url = s; 
    [links addObject:pair]; 
    [pair release]; 
} 

}

+0

你目前的代碼不適合你嗎? – Ether 2011-03-08 22:07:07

+0

@其他我可以通過使用上面的代碼提取@usernames和鏈接,但我一直無法弄清楚如何替換帶有藍色鏈接的文本。 – kbecker 2011-03-09 00:00:51

+0

iOS中的樣式文本目前並不容易。你對Core Text感到滿意嗎,如果這是可行的,但不在3.2之前; Three20爲您的案例設計了標籤,或者您可以考慮使用網頁視圖。 – 2011-03-10 15:43:06

回答

0

你使用什麼類型的ViewController的? 不知道我是否理解你的問題是正確的,但也許它會幫助使用UIWebView來顯示你的數組的內容。

然後,您可以輕鬆地包含一些本地樣式表,以按照您想要的方式設置樣式。