UILabel中生成鏈接並且打開鏈接地址

前段時間遇到一個問題需要把UILabel顯示時遇到網址便自動轉化成網絡鏈接,最終效果如下


實現步驟如下:首先在https://github.com/petrpavlik/PPLabel下載PPLabel並導入到自己的工程文件,然後在需要使用的

#import "PPLabel.h"

添加這個協議:PPLabelDelegate

self.label=[[PPLabel alloc]initWithFrame:CGRectMake(20, 40, SCREEN_WIDTH-40, rect.size.height +5)];

    self.label.delegate=self;

    self.label.text=[_FAG integerValue]==1?_taskmodel.des:_taskupmodel.des;

    

    self.label.font = [UIFont systemFontOfSize:15];

    self.label.textColor = HEXCOLOR(0X888888);

    self.label.numberOfLines=0;

    [view addSubview:self.label];

    

    //self.label=desLabel;

    // self.label.delegate=self;

    

    NSError *error = NULL;

    NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:&error];

    

    self.matches = [detector matchesInString:self.label.text options:0 range:NSMakeRange(0, self.label.text.length)];

    

    [self highlightLinksWithIndex:NSNotFound];




- (void)label:(PPLabel *)label didBeginTouch:(UITouch *)touch onCharacterAtIndex:(CFIndex)charIndex {

    

    [self highlightLinksWithIndex:charIndex];

}


- (void)label:(PPLabel *)label didMoveTouch:(UITouch *)touch onCharacterAtIndex:(CFIndex)charIndex {

    

    [self highlightLinksWithIndex:charIndex];

}


- (void)label:(PPLabel *)label didEndTouch:(UITouch *)touch onCharacterAtIndex:(CFIndex)charIndex {

    

    [self highlightLinksWithIndex:NSNotFound];

    

    for (NSTextCheckingResult *match in self.matches) {

        

        if ([match resultType] == NSTextCheckingTypeLink) {

            

            NSRange matchRange = [match range];

            

            if ([self isIndex:charIndex inRange:matchRange]) {

                

                [[UIApplication sharedApplication] openURL:match.URL];

                break;

            }

        }

    }

    

}


- (void)label:(PPLabel *)label didCancelTouch:(UITouch *)touch {

    

    [self highlightLinksWithIndex:NSNotFound];

}


#pragma mark -


- (BOOL)isIndex:(CFIndex)index inRange:(NSRange)range {

    return index > range.location && index < range.location+range.length;

}


- (void)highlightLinksWithIndex:(CFIndex)index {

    

    NSMutableAttributedString* attributedString = [self.label.attributedText mutableCopy];

    

    for (NSTextCheckingResult *match in self.matches) {

        

        if ([match resultType] == NSTextCheckingTypeLink) {

            

            NSRange matchRange = [match range];

            

            if ([self isIndex:index inRange:matchRange]) {

                [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:matchRange];

            }

            else {

                [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:matchRange];

            }

            

            [attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:matchRange];

        }

    }

    

    self.label.attributedText = attributedString;

}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章