【问题标题】:Unable to use TTTAttributedLabel framework无法使用 TTTAttributedLabel 框架
【发布时间】:2017-04-24 08:36:39
【问题描述】:

我正在使用TTTAttributedLabel 制作UILabel 超链接,我正在尝试这样做:

 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 方法中崩溃,它变成了一个僵尸对象。

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

提前致谢。

【问题讨论】:

  • 你能显示崩溃报告吗
  • 是的,请稍等。
  • 这是我点击 *** -[__NSSingleObjectArrayI valueForKey:]: message sent to deallocated instance 0x178010a40 时得到的结果
  • 嗯,我正在使用与其他人一起运行良好的库,所以我认为不必对他们的库进行任何更改,可以肯定我在使用它时做错了什么。跨度>

标签: ios objective-c tttattributedlabel tttattritubedlabel


【解决方案1】:

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

- (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];
}

【讨论】:

  • 为什么要在视图中添加 tttTermAndCondition?它已经在 .xib 中了。
  • @uzairdhada 我只是在测试你的代码,所以我把标签放在我的 viewDidLoad 中
  • 是的,我可以理解,但我正在尝试通过 .xib 来实现。我的意思是标签已放置在 Xib 中,并在 .h 中创建了它的对象,因此它无法正常工作。
  • 它与故事板配合得很好。可以检查您在哪里将属性中的类名称设置为 TTTAttributedLabel 而不是 UILable
  • Yap 那也很好。你是用 cocoapods 安装框架的吗?
猜你喜欢
  • 1970-01-01
  • 2015-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-19
  • 1970-01-01
相关资源
最近更新 更多