2014-11-21 74 views
0

我正在嘗試在UITextView上設置'常見問題'部分。如何鏈接UITextView上的一行文本,以便當用戶單擊它時,UITextView會滾動到同一視圖中的一段文本。我還想強調文字並將文字顏色更改爲藍色。超鏈接到uitextview中的文本部分

+0

你嘗試過什麼嗎? – Kampai 2014-11-21 10:25:07

+0

爲什麼你想用UITextView來做到這一點? UIWebView或WKWebView是完美的選擇。 – Christian 2014-11-21 10:27:48

+0

我認爲將一個.txt文件加載到從按鈕單擊加載的uitextview中。我不確定是否使用uiwebview或uitextview – BruceC 2014-11-21 14:35:29

回答

0
NSMutableAttributedString * str = [[NSMutableAttributedString alloc] initWithString:@"Google"]; 
[str addAttribute: NSLinkAttributeName value: @"http://www.google.com" range: NSMakeRange(0, str.length)]; 
yourTextField.attributedText = str; 
+0

只有代碼的答案被自動標記爲低質量,因此不鼓勵使用stackoverflow。將來,請詳細說明您的答案,並解釋爲什麼這是問題的解決方案。這有助於其他用戶。 – 2014-11-21 11:52:35

+0

好的下次我會確定... – 2014-11-21 11:54:53

1

嘗試TTTAttributedLabel

TTTAttributedLabel讓您自動檢測日期,地址,鏈接,電話號碼,交通信息鏈接,或允許您嵌入自己的

label.enabledTextCheckingTypes = NSTextCheckingTypeLink; // Automatically detect links when the label text is subsequently changed 
label.delegate = self; // Delegate methods are called when the user taps on a link (see `TTTAttributedLabelDelegate` protocol) 

label.text = @"Fork me on GitHub! (http://github.com/mattt/TTTAttributedLabel/)"; // Repository URL will be automatically detected and linked 

NSRange range = [label.text rangeOfString:@"me"]; 
[label addLinkToURL:[NSURL URLWithString:@"http://github.com/mattt/"] withRange:range]; // Embedding a custom link in a substring 
1

您需要先獲取文本「Frequentry詢問問題」的點擊事件。在點擊事件你需要做滾動代碼。

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange 
{ 

    //Set your character range here 
    // if match return TRUE . 
    // else return FALSE. 

} 

成功的字符範圍獲取通過使用此方法滾動您的textView到問題。

CGPoint bottomOffset; 
bottomOffset = CGPointMake(0,(Y value of the question)); 
[self.chatOutput setContentOffset:bottomOffset animated:YES]; 

此方法將滾動uitextview到您的問題的位置。