首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用TTTAttributedLabel框架

无法使用TTTAttributedLabel框架
EN

Stack Overflow用户
提问于 2017-04-24 16:36:40
回答 1查看 531关注 0票数 0

我正在使用TTTAttributedLabel来制作UILabel超链接,我尝试如下所示:

代码语言:javascript
复制
 NSAttributedString *attString = [[NSAttributedString alloc] initWithString:@"By tapping you agree to the Terms of Use and Privacy Policy."
                attributes:@{
            (id)kCTForegroundColorAttributeName : (id)[UIColor redColor].CGColor,
                                                                                 NSFontAttributeName : [UIFont boldSystemFontOfSize:16],
                                                                                 NSKernAttributeName : [NSNull null],
                                                                                 (id)kTTTBackgroundFillColorAttributeName : (id)[UIColor greenColor].CGColor
                                                                                 }];

// The attributed string is directly set, without inheriting any other text
// properties of the label.
self.tttTermAndCondition.text = attString;
self.tttTermAndCondition.delegate = self; // Delegate methods are called when the user taps on a link (see `TTTAttributedLabelDelegate` protocol)

//self.tttTermAndCondition.text = @"Fork me on GitHub! (http://github.com/mattt/TTTAttributedLabel/)"; // Repository URL will be automatically detected and linked
self.tttTermAndCondition.enabledTextCheckingTypes = NSTextCheckingTypeLink;
NSRange range = [self.tttTermAndCondition.text rangeOfString:@"Terms of Use"];
[self.tttTermAndCondition addLinkToURL:[NSURL URLWithString:@"http://github.com/mattt/"] withRange:range];

这显示得非常好,但当我单击使用术语时,

TTTAtributedLabel在它的- (NSArray *) links方法中崩溃,它成为一个僵尸对象。

我不知道我到底错过了什么。

提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2017-04-24 17:28:08

您好,我将您的代码放在我的示例应用程序中,它可以很好地工作,只需添加委托方法,您的代码就可以正常工作

代码语言:javascript
复制
- (void)viewDidLoad {
[super viewDidLoad];
tttTermAndCondition = [[TTTAttributedLabel alloc]initWithFrame:CGRectMake(0, 50, 320, 200)];
NSAttributedString *attString = [[NSAttributedString alloc] initWithString:@"By tapping you agree to the Terms of Use and Privacy Policy."
                                                                attributes:@{
                                                                             (id)kCTForegroundColorAttributeName : (id)[UIColor redColor].CGColor,
                                                                             NSFontAttributeName : [UIFont boldSystemFontOfSize:16],
                                                                             NSKernAttributeName : [NSNull null],
                                                                             (id)kTTTBackgroundFillColorAttributeName : (id)[UIColor greenColor].CGColor
                                                                             }];

// The attributed string is directly set, without inheriting any other text
// properties of the label.
tttTermAndCondition.text = attString;
tttTermAndCondition.delegate = self; // Delegate methods are called when the user taps on a link (see `TTTAttributedLabelDelegate` protocol)

//self.tttTermAndCondition.text = @"Fork me on GitHub! (http://github.com/mattt/TTTAttributedLabel/)"; // Repository URL will be automatically detected and linked
tttTermAndCondition.enabledTextCheckingTypes = NSTextCheckingTypeLink;
NSRange range = [tttTermAndCondition.text rangeOfString:@"Terms of Use"];
[tttTermAndCondition addLinkToURL:[NSURL URLWithString:@"http://github.com/mattt/"] withRange:range];
//self.se
[self.view addSubview:tttTermAndCondition];
// Do any additional setup after loading the view, typically from a nib.
}


-(void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url
{
    [[UIApplication sharedApplication] openURL:url options:nil completionHandler:nil];
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43583345

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档