iOS_带下划线的Label

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】

 












评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值