2012-10-24 84 views
2

我正在開發一個應用程序,以便從遠程Web服務(如Twitter)接收消息。有時這些消息包含URL:s,我想讓這些可點擊。我想知道哪種方法最好。我已經嘗試過NSLinkAttributeName,但它不適用於iOS。是否有可能創建一個透明的按鈕,並將其放置在textview的正確位置上方?我怎麼能這樣做?還是有更好/更簡單的方法?網址的位置和長度可能會有所不同。NSMutableAttributedString中的可點擊網址iOS6

回答

3

還有的要對這個兩個好辦法:

1 CoreText,或者更確切地說,一個包裝圍繞它喜歡TTTAtributedLabel

這是最強大的解決方案。它支持Datadetectortypes(儘管你最好在顯示它之前自己檢測性能),它比較流行。

但它可能會影響你的tableview的滾動性能取決於你的使用情況。

2.動態放置按鈕上方的標籤就像IFTweetLabel

這是最簡單的辦法。 IFTweetLabel基本上是UILabel的一個插件,它的表現非常出色。但是,如果您想要對其進行過多定製,您最終會遇到其限制。更何況它不能完美地表現出完美的表現。因人而異。

+0

謝謝,決定去TTTAtributedLabel。 – Anders

2

一個選項是使用UIDataDetectorTypeLink。勾選此link for more details.

// Create textview, centering horizontally 
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 10, 320, 50)]; 

// Set font, background and alignment 
[textView setFont:[UIFont boldSystemFontOfSize:16]]; 
[textView setBackgroundColor:[UIColor blackColor]]; 
[textView setTextAlignment:UITextAlignmentCenter]; 

// Unfortunately the link will show as blue even with this setting 
// [textView setTextColor:[UIColor greenColor]]; 

// Edit and scrolling off 
[textView setEditable:NO]; 
[textView setScrollEnabled:NO]; 

// Set data type to specify URL/link 
[textView setDataDetectorTypes:UIDataDetectorTypeLink]; 

// Set text as URL 
[textView setText:@"text.com"]; 

[self.view addSubview:textView]; 

一些其他選項UIWebview與委託來處理在它的URL聯繫。而另一種選擇是fancy UILabels

對於你問的其他問題,請在此How to open a UITextView URL in UI Web View?Is there a way to create custom UIDataDetectorTypes?Change color of UITextView links with a filter?

+0

謝謝,UIDataDetectorTypeLink似乎對聯繫工作。但我想知道是否有可能改變鏈接的外觀,或者更具體地說是改變顏色。還改變,所以它打開一個自定義UIWebView而不是Safari?是它 - 通過繼承UITextView - 可以創建自定義的UIDataDetectorTypes,比如@mentions和#hashtags。 – Anders

+0

檢查本作,http://stackoverflow.com/questions/1555294/how-to-open-a-uitextview-url-in-ui-web-view,http://stackoverflow.com/questions/2358343/is -there-a-way-to-create-custom-uidatadetectortypes,以及用過濾器更改UITextView鏈接的顏色? ..我用這些鏈接更新了我的答案。 – iDev