#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(一) 初步使用
最新推荐文章于 2024-03-22 09:54:38 发布