iOS学笔记本45- TTTAttributedLabel 使用方法

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


#import "ViewController.h"


@interface ViewController () <TTTAttributedLabelDelegateUIActionSheetDelegate>


@end


@implementation ViewController


static inline NSRegularExpression * NameRegularExpression() {

    static NSRegularExpression *_nameRegularExpression = nil;

    

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        _nameRegularExpression = [[NSRegularExpression allocinitWithPattern:@"^\\w+" options:NSRegularExpressionCaseInsensitive error:nil];

    });

    

    return _nameRegularExpression;

}


- (void)viewDidLoad {

    [super viewDidLoad];

    

    TTTAttributedLabel *label = [[TTTAttributedLabel allocinitWithFrame:CGRectMake(10012012060)];

    label.font = [UIFont systemFontOfSize:14];

    label.textColor = [UIColor blackColor];

    label.lineBreakMode = NSLineBreakByCharWrapping;

    label.numberOfLines = 0;

    //设置高亮颜色

    label.highlightedTextColor = [UIColor greenColor];

    

    //检测url

    label.enabledTextCheckingTypes = NSTextCheckingTypeLink;

    

    //对齐方式

    label.verticalAlignment = TTTAttributedLabelVerticalAlignmentCenter;

    

    //行间距

    label.lineSpacing = 8;

    

    //设置阴影

    label.shadowColor = [UIColor grayColor];

    

    label.delegate = self// Delegate

    //NO 不显示下划线

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

    

    NSString *text = @"冷清风 赞了 王战 的说说";


    

    [label setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString *(NSMutableAttributedString*mutableAttributedString)

    {

        

        //设置可点击文字的范围

        NSRange boldRange = [[mutableAttributedString stringrangeOfString:@"冷清风" options:NSCaseInsensitiveSearch];

        

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

        UIFont *boldSystemFont = [UIFont boldSystemFontOfSize:16];

        CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSizeNULL);

    

        if (font) {

            

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

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

            

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

            [mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[[UIColor blueColorCGColorrange:boldRange];


            CFRelease(font);

            

        }

       return mutableAttributedString;

    }];

    

    //正则

    NSRegularExpression *regexp = NameRegularExpression();

    

    NSRange linkRange = [regexp rangeOfFirstMatchInString:text options:0 range:NSMakeRange(03)];

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.exiucai.com/"]];

    

    //设置链接的url

    [label addLinkToURL:url withRange:linkRange];

    

    

    [self.view addSubview:label];

}


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

   didSelectLinkWithURL:(NSURL *)url

{

    

    [[[UIActionSheet allocinitWithTitle:[url absoluteStringdelegate:self cancelButtonTitle:NSLocalizedString(@"Cancel"nildestructiveButtonTitle:nilotherButtonTitles:NSLocalizedString(@"Open Link in Safari"nil), nilshowInView:self.view];

}


#pragma mark - UIActionSheetDelegate


- (void)actionSheet:(UIActionSheet *)actionSheet

clickedButtonAtIndex:(NSInteger)buttonIndex

{

    if (buttonIndex == actionSheet.cancelButtonIndex) {

        return;

    }

    

    [[UIApplication sharedApplicationopenURL:[NSURL URLWithString:actionSheet.title]];

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值