Arabic and english characters in uilabel

784 Views Asked by At

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

There are 1 best solutions below

3
On

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.