为什么我使用“usedRectForTextContainer”无法获取textContainer的rect?

4

我在这个问题(如何在UILabel的NSAttributedString中创建可点击的“链接”?)中通过NAlexN的回答得到了答案,并自己编写了一个演示。然而,不幸的是,我无法使演示正常工作。以下是我的代码(每个人都可以将以下代码直接复制到新项目中进行测试)。

#import "ViewController.h"

@interface ViewController ()
{
    UILabel* label;

    NSTextContainer *textContainer;
    NSLayoutManager *layoutManager;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [super viewDidLoad];
    label=[[UILabel alloc]initWithFrame:CGRectMake(15, 30, 350, 300)];
    label.backgroundColor=[UIColor greenColor];
    label.userInteractionEnabled = YES;
    UITapGestureRecognizer*  tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapOnLabel:)];
    [tap setNumberOfTapsRequired:1];
    [label addGestureRecognizer:tap];
    [self.view addSubview:label];

    NSMutableAttributedString *attributedString =[[NSMutableAttributedString alloc] initWithString:@"String with a link" attributes:nil];
    NSRange linkRange = NSMakeRange(14, 4); // for the word "link" in the string above
    NSDictionary *linkAttributes = @{ NSForegroundColorAttributeName : [UIColor colorWithRed:0.05 green:0.4 blue:0.65 alpha:1.0],NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle) };
    [attributedString setAttributes:linkAttributes range:linkRange];

    // Assign attributedText to UILabel
    label.attributedText = attributedString;

    // Create instances of NSLayoutManager, NSTextContainer and NSTextStorage
    layoutManager = [[NSLayoutManager alloc] init];
    textContainer = [[NSTextContainer alloc] initWithSize:CGSizeZero];
    NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:attributedString];

    // Configure layoutManager and textStorage
    [layoutManager addTextContainer:textContainer];
    [textStorage addLayoutManager:layoutManager];


    // Configure textContainer
    textContainer.lineFragmentPadding = 0.0;
    textContainer.lineBreakMode = label.lineBreakMode;
    textContainer.maximumNumberOfLines = label.numberOfLines;
}

//each time the label changes its frame, update textContainer's size:
- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    textContainer.size = label.bounds.size;
    NSLog(@"textContainer.size=%@",NSStringFromCGSize(textContainer.size));
}

- (void)handleTapOnLabel:(UITapGestureRecognizer *)tapGesture
{
    NSLog(@"Tap received");

    CGPoint locationOfTouchInLabel = [tapGesture locationInView:tapGesture.view];
    CGSize labelSize = tapGesture.view.bounds.size;

    CGRect textBoundingBox = [layoutManager usedRectForTextContainer:textContainer];
      NSLog(@"textBoundingBox=%@",NSStringFromCGRect(textBoundingBox));

    CGPoint textContainerOffset = CGPointMake((labelSize.width - textBoundingBox.size.width) * 0.5 - textBoundingBox.origin.x,(labelSize.height - textBoundingBox.size.height) * 0.5 - textBoundingBox.origin.y);
    CGPoint locationOfTouchInTextContainer = CGPointMake(locationOfTouchInLabel.x - textContainerOffset.x,locationOfTouchInLabel.y - textContainerOffset.y);
    NSInteger indexOfCharacter = [layoutManager characterIndexForPoint:locationOfTouchInTextContainer inTextContainer:textContainer fractionOfDistanceBetweenInsertionPoints:nil];
    NSRange linkRange = NSMakeRange(14, 4); // it's better to save the range somewhere when it was originally used for marking link in attributed string
    if (NSLocationInRange(indexOfCharacter, linkRange))
    {
        // Open an URL, or handle the tap on the link in any other way
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://stackoverflow.com/"]];
        NSLog(@" Link was taped ");
    }
}

@end

运行结果如下:

2016-03-11 10:58:04.457 [13451:1007618] textContainer.size={350, 300}

2016-03-11 10:58:07.968 [13451:1007618] 收到了点击

2016-03-11 10:58:07.969 [13451:1007618] textBoundingBox={{0, 0}, {0, 0}}

从运行结果来看,layoutManager似乎没有起作用。我不知道为什么,可能是我使用layoutManager的方式不对。请有经验的人帮忙解决这个问题。非常感谢!
P.S:此示例旨在创建可点击的url链接,放置在UILabel的NSAttributedText中。
2个回答

7

虽然距离OP提问已经过去五年,但这个回答可能对其他人仍有所帮助。

问题可能是由于textBoundingBox的矩形高度和宽度为0。尝试强制layoutManager预先正确布局文本,例如:

[layoutManager ensureLayoutForGlyphRange:NSMakeRange(0, attributedString.length)];

2
在你的代码中,textStorage 将被释放。因此,请将 textStorage 保留在您的 ViewController 中,这样它就可以正常工作。
@interface ViewController ()
{
    UILabel* label;

    NSTextContainer *textContainer;
    NSLayoutManager *layoutManager;
    NSTextStorage  *textStorage;
}
@end
....
// Create instances of NSLayoutManager, NSTextContainer and NSTextStorage
layoutManager = [[NSLayoutManager alloc] init];
textContainer = [[NSTextContainer alloc] initWithSize:CGSizeZero];
textStorage = [[NSTextStorage alloc] initWithAttributedString:attributedString];

// Configure layoutManager and textStorage
[layoutManager addTextContainer:textContainer];
[textStorage addLayoutManager:layoutManager];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接