TTTAttributedLabel使用介绍(转)

 TTTAttributedLabel 库地址 https://github.com/TTTAttributedLabel/TTTAttributedLabel

可以实现电话  地址  链接自动查找显示等

    //可以自动检测url

    label.enabledTextCheckingTypes = NSTextCheckingTypeLink;

    

    label.delegate = self; // Delegate

    //NO 不显示下划线

//    label.linkAttributes = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:(NSString *)kCTUnderlineStyleAttributeName];

    

    [label setText:bm.Content afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString)

     {

//         //设定可点击文字的的大小

//         UIFont *boldSystemFont = [UIFont boldSystemFontOfSize:16];

//         CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);

//         

//         if (result) {

//             

//             //设置可点击文本的大小

//             [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)font range:result.range];

//             

//             //设置可点击文本的颜色

//             [mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[[UIColor blueColor] CGColor] range:result.range];

//             CFRelease(font);

//

//         }

         return mutableAttributedString;

     }];

   NSString *urlString = [[bm.Content substringWithRange:result.range] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];

    NSURL *url = [[NSURL alloc]initWithString:urlString];

    

    //设置链接的url

    [label addLinkToURL:url withRange:result.range];

 }

//链接点击事件

- (void)attributedLabel:(__unused TTTAttributedLabel *)label

   didSelectLinkWithURL:(NSURL *)url

{

    NSString *str = [url absoluteString];

    NSString *decodeStr = [str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    if ([[decodeStr substringToIndex:1] isEqualToString:@"#"]) {

    }

    else

    {

    //为链接 打开链接

    [[[UIActionSheet alloc] initWithTitle:[url absoluteString] delegate:self cancelButtonTitle:NSLocalizedString(@"取消", nil) destructiveButtonTitle:nil otherButtonTitles:NSLocalizedString(@"在浏览器中打开连接", nil), nil] showInView:self.view];

    }

}

http://blog.csdn.net/lengshengren/article/details/43971441

原文地址:https://www.cnblogs.com/qiutangfengmian/p/4962282.html