[英]hyperlink to section of text in uitextview
我正在嘗試在UITextView上設置“常見問題”部分。 如何鏈接UITextView上的一行文本,以便當用戶單擊它時,UITextView滾動到同一視圖中的一部分文本。 我還想在文本下划線並將文本顏色更改為藍色。
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
您需要首先獲取文本“常見問題”的點擊事件。 在click事件上,您需要編寫滾動代碼。
- (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滾動到您問題的位置。
NSMutableAttributedString * str = [[NSMutableAttributedString alloc] initWithString:@"Google"];
[str addAttribute: NSLinkAttributeName value: @"http://www.google.com" range: NSMakeRange(0, str.length)];
yourTextField.attributedText = str;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.