iOS_带下划线的Label_带点击和长按事件
工程下载:点击打开链接
【一】【PCH创建步骤】
1. 在Supporting Files下面,右键 New File, 选择 Other 里面的PCH File 2. 点击相应的TARGETS --->Build Setting ,搜索Prefix Header 3. 将Precompile Prefix Header 勾选为YES 4. 将左上角 项目的名字拷贝一下, 双击Prefix Header一栏, 填入:【项目的名字/Beyond.pch】
#import <Availability.h>
#ifndef __IPHONE_3_0
#warning "This project uses features only available in iOS SDK 3.0 and later."
#endif
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#endif
【2】【添加libxml库】
工程-->targets-->Build Phases-->添加libxml2.dylib
【3】【解决libxml 找不到的问题】
导入了libxml2.dylib,但是却提示libxml/XXX.h file not found,那是因为路径无法找到这个libxml2.dylib框架
工程-->targets-->Build Settings,查找:“Header search paths”
在Header search paths 输入:${SDK_DIR}/usr/include/libxml2
【4】【将整个LinkLabel拖进项目中】
【5】【控制器里的使用示例代码】
#import "CXAHyperlinkLabel.h" #import "NSString+CXAHyperlinkParser.h" @interface BeyondCtrl () { CXAHyperlinkLabel *_label; } @end
- (void)viewDidLoad { [super viewDidLoad]; _label = (CXAHyperlinkLabel *)self.lastView; NSArray *URLs; NSArray *URLRanges; NSAttributedString *as = [self attributedString:&URLs URLRanges:&URLRanges]; // _label = [[CXAHyperlinkLabel alloc] initWithFrame:CGRectZero]; _label.numberOfLines = 0; _label.backgroundColor = [UIColor clearColor]; _label.attributedText = as; [_label setURLs:URLs forRanges:URLRanges]; _label.URLClickHandler = ^(CXAHyperlinkLabel *label, NSURL *URL, NSRange range, NSArray *textRects){ [[[UIAlertView alloc] initWithTitle:@"URLClickHandler" message:[NSString stringWithFormat:NSLocalizedString(@"Click on the URL %@", nil), [URL absoluteString]] delegate:nil cancelButtonTitle:NSLocalizedString(@"Dismiss", nil) otherButtonTitles:nil] show]; }; _label.URLLongPressHandler = ^(CXAHyperlinkLabel *label, NSURL *URL, NSRange range, NSArray *textRects){ [[[UIActionSheet alloc] initWithTitle:[NSString stringWithFormat:@"URLLongPressHandler for URL: %@", [URL absoluteString]] delegate:nil cancelButtonTitle:NSLocalizedString(@"Cancel", nil) destructiveButtonTitle:nil otherButtonTitles:nil] showInView:self.view]; }; } #pragma mark - privates - (NSAttributedString *)attributedString:(NSArray *__autoreleasing *)outURLs URLRanges:(NSArray *__autoreleasing *)outURLRanges { NSString *HTMLText = @"An inline link may display a modified version of the content; for instance, instead of an image, a <a href='/wiki/Thumbnail' title='Thumbnail'>thumbnail</a>, <a href='/wiki/Image_resolution' title='Image resolution'>low resolution</a> <a href='/wiki/Preview_(computing)' title='Preview (computing)'>preview</a>, <a href='/wiki/Cropping_(image)' title='Cropping (image)'>cropped</a> section, or <a href='/wiki/Magnification' title='Magnification'>magnified</a> section may be shown. The full content will then usually be available on demand, as is the case with <a href='/wiki/Desktop_publishing' title='Desktop publishing'>print publishing</a> software – e.g. with an external link. This allows for smaller file sizes and quicker response to changes when the full linked content is not needed, as is the case when rearranging a <a href='/wiki/Page_layout' title='Page layout'>page layout</a>."; NSArray *URLs; NSArray *URLRanges; UIColor *color = [UIColor blackColor]; UIFont *font = [UIFont fontWithName:@"Baskerville" size:19.]; NSMutableParagraphStyle *mps = [[NSMutableParagraphStyle alloc] init]; mps.lineSpacing = ceilf(font.pointSize * .5); NSShadow *shadow = [[NSShadow alloc] init]; shadow.shadowColor = [UIColor whiteColor]; shadow.shadowOffset = CGSizeMake(0, 1); NSString *str = [NSString stringWithHTMLText:HTMLText baseURL:[NSURL URLWithString:@"http://en.wikipedia.org/"] URLs:&URLs URLRanges:&URLRanges]; NSMutableAttributedString *mas = [[NSMutableAttributedString alloc] initWithString:str attributes:@ { NSForegroundColorAttributeName : color, NSFontAttributeName : font, NSParagraphStyleAttributeName : mps, NSShadowAttributeName : shadow, }]; [URLRanges enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){ [mas addAttributes:@ { NSForegroundColorAttributeName : [UIColor blueColor], NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle) } range:[obj rangeValue]]; }]; *outURLs = URLs; *outURLRanges = URLRanges; return [mas copy]; }
【6】【设置Label的UserInteraction为YES】