您的位置:首页 > 其它

TYAttributedLabel(一) 初步使用

2016-08-12 17:06 253 查看
   
#import "ViewController.h"
#import "TYAttributedLabel.h"
#import "TYTextStorage.h"

@interface ViewController ()<TYAttributedLabelDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

TYAttributedLabel *label = [[TYAttributedLabel alloc]initWithFrame:CGRectMake(0, 100, CGRectGetWidth(self.view.frame), 0)];

label.delegate = self;

NSString *text = @"\t总有一天你将破蛹而出,成长得比人们期待的还要美丽。\n";
[label setText:text];

TYTextStorage *textStorage = [[TYTextStorage alloc]init];
textStorage.range = [text rangeOfString:@"总有一天你将破蛹而出"];
textStorage.textColor = [UIColor orangeColor];
textStorage.font = [UIFont systemFontOfSize:16];
[label addTextStorage:textStorage];

TYLinkTextStorage *linkStorage = [[TYLinkTextStorage alloc]init];
linkStorage.range = [text rangeOfString:@"期待"];
[label addTextStorage:linkStorage];

[label sizeToFit];
[self.view addSubview:label];
}

- (void)attributedLabel:(TYAttributedLabel *)attributedLabel textStorageClicked:(id<TYTextStorageProtocol>)textStorage atPoint:(CGPoint)point{

NSRange rang = [textStorage range];
NSString *msg = attributedLabel.text;
msg = [msg substringWithRange:rang];
NSLog(@"---->%@",msg);
if ([msg isEqualToString:@"期待"]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.baidu.com/"]];

}
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  TYAttributedLabel一