Why do I fail to get textContainer's rect by using “usedRectForTextContainer”

血红的双手。 提交于 2019-12-11 00:41:31

问题


I learned the answer by NAlexN in this Question (Create tap-able "links" in the NSAttributedString of a UILabel?), and write a demo by myself. However, it is unluck for me to work the demo out. Here is my code: (everyone can copy following code to a new project directly to make a test)

#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

The result of running is:

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

2016-03-11 10:58:07.968 [13451:1007618] Tap received

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

From the running result, the layoutManager seem doesn't work. I don't figour out why, maybe I used the layoutManager in a wrong way. Could any body help with this issue. Thank you so Much!

P.S: This demo is designed to create a tap-able url-link in the NSAttributedText of a UILabel.


回答1:


(Five years late for the OP but this might still be of help to someone.)

The problem might be because the textBoundingBox has a rect of 0 height and width. Try forcing the layoutManager to layout the text correctly beforehand, e.g.

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



回答2:


In your code, the textStorage will be released. So keep textStorage by your ViewController, It can work.

@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];


来源:https://stackoverflow.com/questions/35931789/why-do-i-fail-to-get-textcontainers-rect-by-using-usedrectfortextcontainer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!