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;

}




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