TextView添加鏈接實現富文本label帶點擊功能

TextView添加鏈接

有時候我們需要實現一行文中中某些文字帶有點擊的功能,大多數情況下會採用label和button結合實現或者第三方富文本label,其實用textView添加鏈接實現起來非常方便。
實現如下效果:
img
需要設置得文字

NSString *text =@"我同意《XX在線服務協議》及《XX在線用戶信息及隱私保護規則》";
NSMutableAttributedString *MAttributedString = [[NSMutableAttributedString alloc] initWithString:text];
    //《XX在線服務協議》
    NSRange range1 = NSMakeRange(3, 10);
    [MAttributedString addAttributes:@{NSLinkAttributeName:[NSURL URLWithString:@"string1://"]                                            } range:range1];
    //《XX在線用戶信息及隱私保護規則》
    NSRange range2 = NSMakeRange(14, 17);
    [MAttributedString addAttributes:@{NSLinkAttributeName:[NSURL URLWithString:@"string2://"]}range:range2];
    [MAttributedString endEditing];

textView的設置

    UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(20,100, SCREEN_WIDTH  - 20,50)];
    textView.font = [UIFont systemFontOfSize:20];
    textView.scrollEnabled = NO;
    textView.textContainerInset = UIEdgeInsetsMake(0, 0,2,0);
    textView.text = text;
    textView.textColor = [UIColor blackColor];
    //必須設爲NO不然不能響應點擊事件
    textView.editable = NO;
    //設置鏈接的屬性 設置那一段顏色
    NSDictionary *linkAttributes =@{
                                    NSForegroundColorAttributeName:[UIColor redColor]};
    textView.linkTextAttributes = linkAttributes;

    /** 設置自動檢測類型爲鏈接網址. */
    textView.dataDetectorTypes = UIDataDetectorTypeAll;
    textView.delegate = self;
    textView.attributedText = MAttributedString;
    [self.view addSubview:textView];

那兩段文字的點擊事件,在UItextView Delegate中實現

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
{
    // 在代理方法回調的時候,如果沒有“://”的話,在[URL scheme]方法裏取不到特定的值,一句話://前面的纔是最重要的標識符
    if ([[URL scheme] rangeOfString:@"string1"].location != NSNotFound) {
        NSLog(@"點擊《XX在線服務協議》");
        return NO;
    }
    else if ([[URL scheme] rangeOfString:@"string2"].location != NSNotFound){
        NSLog(@"點擊《XX在線用戶信息及隱私保護規則》");
        return NO;
    }
    return YES;
}

swift實現

        let text = "我同意《優投在線服務協議》及《優投在線用戶信息及隱私保護規則》" 
        let attributedString = NSMutableAttributedString(string:text)
        let range1 = NSMakeRange(3, 10)
        let range2 = NSMakeRange(14, 17)

        attributedString.addAttribute(NSLinkAttributeName, value: NSURL(string:"st1://")!, range: range1);
        attributedString.addAttribute(NSLinkAttributeName, value: NSURL(string:"st2://")!, range: range2);
        attributedString.endEditing();

        let textView = UITextView(frame:CGRect(x: 20, y: 100, width: UIScreen.main.bounds.size.width, height: 50))
        textView.font = UIFont.systemFont(ofSize: 20)
        textView.isScrollEnabled = false
        textView.isEditable = false
        textView.text = text
        textView.textColor = UIColor.black;
        textView.linkTextAttributes = [NSForegroundColorAttributeName:UIColor.red]
        textView.dataDetectorTypes = .all;
        textView.delegate = self;
        textView.attributedText = attributedString
        self.view.addSubview(textView);
    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {

        if ((URL.scheme?.range(of: "st1")) != nil) {
            print("st1");
            return false;
        }
        else if ((URL.scheme?.range(of: "st2")) != nil){
            print("st2");
            return false;
        }

        return true
    }
發佈了23 篇原創文章 · 獲贊 1 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章