2009-07-23 21 views
7

我想要的只是在我的viewController中顯示一些簡單的文本,並自動超鏈接。當用戶點擊鏈接時,我希望控件以某種方式進行回調,在那裏我可以對URL進行一些操作。我怎樣才能做到這一點?只是如何使用TTStyledTextLabel?

我已經瀏覽了TTCatalog數小時。我也嘗試過查看three20的源代碼以及查看堆棧跟蹤。沒有幫助。我無法弄清楚我的應用程序如何對網址的點擊作出反應。請提供任何提示?

+1

爲什麼你不使用的UIWebView?當你點擊webView中的鏈接時,UIWebViewDelegate獲取shouldStartLoadWithRequest消息 – oxigen 2009-07-23 14:31:59

回答

11

努力幫助沒有看到你已經嘗試過,但你應該能夠做到像下面這樣:

TTStyledTextLabel* label = [[[TTStyledTextLabel alloc] 
     initWithFrame:someFrame] autorelease]; 
NSString* labelText = @"This should <a href=\"custom-uri://some/url\">work</a>"; 
label.text = [TTStyledText textFromXHTML:labelText lineBreaks:NO URLs:YES]; 
[someView addSubview:label]; 

然後,您可以在使用TTNavigatorTTURLMap映射custom-uri://some/url到一個特定的控制器你的應用程序,或者在應用程序委託中自己處理它。瞭解如何做到這一點的最佳方法是查看Three20源文件中包含的示例應用程序TTNavigatorDemo。具體而言,請查看AppDelegate.m,這是所有URL映射執行的位置。

+0

偉大的開始!你可以舉一個簡單的例子來說明如何使用TTNavigator和TTURLMap來說明當URL被點擊時推送控制器嗎? – erotsppa 2009-07-23 16:23:07

0

除了Nathan關於URL映射和鏈接的說法之外,您還可以使用CSS樣式!

TTStyledTextLabel* label = [[[TTStyledTextLabel alloc] initWithFrame:someFrame] autorelease]; 
NSString* labelText = @"This should <a href=\"custom-uri://some/url\">work</a> and 
<span class=\"redText\">this should be red</span>"; 
label.text = [TTStyledText textFromXHTML:labelText lineBreaks:NO URLs:YES]; 
[someView addSubview:label]; 

然後在你的StyleSheet.m實施

- (TTStyle*) redText { 
    return [TTTextStyle styleWithFont:[UIFont systemFontOfSize:12] color:RGBCOLOR(255,0,0) next:nil]; 
}