2014-11-21 75 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到您的问题的位置。