0

I use:

    - (void)handleLinksTouch:(CGPoint)touchPoint {
        NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:self.attributedString];

        NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
        [textStorage addLayoutManager:layoutManager];

        NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:self.frame.size];
        textContainer.lineFragmentPadding = 0;

        [layoutManager addTextContainer:textContainer];

        NSUInteger charIndex = [layoutManager characterIndexForPoint:touchPoint inTextContainer:textContainer fractionOfDistanceBetweenInsertionPoints:NULL];

        NSDictionary *charAttributes = [self.attributedString attributesAtIndex:charIndex effectiveRange:NULL];
        TVMessageLink *link = charAttributes[TVLinkAttributeName];

        if ([self.delegate respondsToSelector:@selector(linkLabel:didSelectLink:)]) { 
            [self.delegate linkLabel:self didSelectLink:link];
        } 
    }

for determination of touching on some link in my attributed text in UILabel. If I use only English characters in a string, all is well - the character that I've touched determining precisely. But if I try to combine Arabic and English in one label as in the screen enter image description here

and try to touch in aljaml.com I get the range of amnesty.com. What should I do?

1 Answers1

0

Your solution works based on assuming UILabel using same layout options with you, it's not safe. Try UITextView / TTTAttributedLabel or My GSAttributedLabel for link interaction. Hoping to be helpful.

Geansea
  • 26
  • 2
  • What layout options are you talking about? – Илья Терезников Aug 24 '17 at 14:41
  • For example, on old version of iOS, UILabel maybe using CoreText for layouting instead of TextKit(NSLayoutManager). Or it sets some default line spacing / kerning supporting / ... internally. In your case, it returns wrong result only when adding Arabic, there maybe some difference on font fallback algorithm. That is, if a character cannot be found in current font (English font), search other fonts in a given order. Above all, since we do not known how UILabel really works, solutions based on guess is not safe. – Geansea Aug 25 '17 at 01:12
  • It's default system font, and I don't think that problem can be connected with it. And there are no other additional custom attributes (line spacing / kerning supporting and other) that can break this algorithm. I think the problem lies elsewhere but now I can't find it. – Илья Терезников Aug 25 '17 at 08:32