Tap on a part of text of UILabel
<p>I have a problem that <code>boundingRectForGlyphRange</code> always returns <code>CGRect.zero</code> &quot;0.0, 0.0, 0.0, 0.0&quot;.</p>
<p>For example, I am coding for touching on a part of text of <code>UILabel</code> feature. My text has first part is <strong>any text</strong> and second one is <strong>READ MORE</strong>.</p>
<p>I want the tap recognizer only work when I touch <strong>READ MORE</strong>. If I touch on any point on <code>UILabel</code>, <code>CGRectContainsPoint</code> always return <code>true</code>, then the action called.</p>
<p>Here my code:</p>
<pre><code>override func viewDidLoad() {
        super.viewDidLoad()
        
        // The full string
        
        let firstPart:NSMutableAttributedString = NSMutableAttributedString(string: &quot;Lorem ipsum dolor set amit &quot;, attributes: [NSFontAttributeName: UIFont.systemFontOfSize(13)])
        firstPart.addAttribute(NSForegroundColorAttributeName, value: UIColor.blackColor(),
            range: NSRange(location: 0, length: firstPart.length))
        info.appendAttributedString(firstPart)
        
        // The &quot;Read More&quot; string that should be touchable
        let secondPart:NSMutableAttributedString = NSMutableAttributedString(string: &quot;READ MORE&quot;, attributes: [NSFontAttributeName: UIFont.systemFontOfSize(14)])
        secondPart.addAttribute(NSForegroundColorAttributeName, value: UIColor.blackColor(),
            range: NSRange(location: 0, length: secondPart.length))
        info.appendAttributedString(secondPart)
        
        lblTest.attributedText = info
        
        // Store range of chars we want to detect touches for
        moreStringRange = NSMakeRange(firstPart.length, secondPart.length)
        print(&quot;moreStringRange\(moreStringRange)&quot;)
        
        tapRec.addTarget(self, action: &quot;didTap:&quot;)
        lblTest.addGestureRecognizer(tapRec)
        
    }


    func didTap(sender:AnyObject) {
        // Storage class stores the string, obviously
        let textStorage:NSTextStorage = NSTextStorage(attributedString: info)
        // The storage class owns a layout manager
        let layoutManager:NSLayoutManager = NSLayoutManager()
        textStorage.addLayoutManager(layoutManager)
        
        // Layout manager owns a container which basically
        // defines the bounds the text should be contained in
        let textContainer:NSTextContainer = NSTextContainer(size: lblTest.frame.size)
        textContainer.lineFragmentPadding = 0
        textContainer.lineBreakMode = lblTest.lineBreakMode
        
        // Begin computation of actual frame
        // Glyph is the final display representation
        var glyphRange = NSRange()
        // Extract the glyph range
        layoutManager.characterRangeForGlyphRange(moreStringRange!, actualGlyphRange: &amp;glyphRange)
        
        // Compute the rect of glyph in the text container
        print(&quot;glyphRange\(glyphRange)&quot;)
        print(&quot;textContainer\(textContainer)&quot;)
        let glyphRect:CGRect = layoutManager.boundingRectForGlyphRange(glyphRange, inTextContainer: textContainer)
        
        // Final rect relative to the textLabel.
        print(&quot;\(glyphRect)&quot;)
        
        // Now figure out if the touch point is inside our rect
        let touchPoint:CGPoint = tapRec.locationOfTouch(0, inView: lblTest)
        
        if CGRectContainsPoint(glyphRect, touchPoint) {
            print(&quot;User tapped on Read More. So show something more&quot;)
        }
    }
}
</code></pre>