将子字符串作为超链接的最佳工作方法是什么?

时间:2017-04-24 18:17:59

标签: ios objective-c tttattributedlabel

我正在使用TSLabel。它适用于iOS 10.2及更低版本,但在iOS 10.3中无效。我也尝试使用TTTAttribuedLabel,但由于某种原因,框架在其中一个方法中崩溃。现在我被卡住了。我不知道究竟应该用什么。在TSLabel中有一个方法

- (void) layoutManager: (NSLayoutManager *) layoutManager didCompleteLayoutForTextContainer: (NSTextContainer *) textContainer atEnd: (BOOL)layoutFinishedFlag {
// search for our custom label attribute - if we have it we'll tell it about link bounds!
TSLabel* label = [[self attribute: TSLabelAttributeName
                         atIndex: 0
                  effectiveRange: nil] object];

if ( label != nil && [label isKindOfClass: [TSLabel class]] )
{
    CGRect containerGlyphBounds = [layoutManager boundingRectForGlyphRange: [layoutManager glyphRangeForTextContainer: textContainer] inTextContainer: textContainer];

    // determine the bounds of each link and record that information with the TSLabel
    NSMutableSet* links = [NSMutableSet new];
    [self enumerateAttribute: TSLinkAttributeName
                     inRange: NSMakeRange(0, self.length)
                     options: 0
                  usingBlock: ^(NSURL* url, NSRange range, BOOL *stop) {

                      if ( url != nil )
                      {
                          TSLinkInfo* link = [TSLinkInfo new];
                          link.url = url;
                          link.range = range;

                          NSRange glyphRange = [layoutManager glyphRangeForCharacterRange: range actualCharacterRange: nil];
                          CGRect bounds = [layoutManager boundingRectForGlyphRange: glyphRange inTextContainer: textContainer];
                          link.bounds = CGRectOffset(bounds, 0, (label.bounds.size.height-containerGlyphBounds.size.height)/2);

                          [links addObject: link];
                      }
                  }];

    label.links = links;
}
}

仅在iOS 10.3和TTTAtributedLabel的情况下不会调用它。如果我创建新项目并将相同的代码放在我的项目中,框架工作正常。这个TTTAttributedLabel只会在我的项目中崩溃。它在以下方法中崩溃:

- (NSArray *) links {
return [_linkModels valueForKey:@"result"];
}

LinkModels变为NSZombie

0 个答案:

没有答案