Swift : Tippen Sie auf einen Teil des Textes von UILabel

Ich habe ein problem, dass "boundingRectForGlyphRange" immer wieder CGRect.null "0.0, 0.0, 0.0, 0.0". "boundingRectForGlyphRange" nicht funktioniert. So bin ich zum Beispiel der Codierung für berühren auf einen Teil des Textes der UILabel-Funktion. Mein text hat erste Teil ist "beliebiger text" und das zweite ist "LESEN Sie MEHR". Ich will die tap-Erkennung nur funktioniert, wenn ich berühren Sie "MEHR LESEN". Wenn ich Tippen Sie auf einen beliebigen Punkt auf UILabel, "CGRectContainsPoint" immer "true" zurückgeben,dann wird die Aktion genannt

Hier mein code:

override func viewDidLoad() {
        super.viewDidLoad()

        //The full string

        let firstPart:NSMutableAttributedString = NSMutableAttributedString(string: "Lorem ipsum dolor set amit ", attributes: [NSFontAttributeName: UIFont.systemFontOfSize(13)])
        firstPart.addAttribute(NSForegroundColorAttributeName, value: UIColor.blackColor(),
            range: NSRange(location: 0, length: firstPart.length))
        info.appendAttributedString(firstPart)

        //The "Read More" string that should be touchable
        let secondPart:NSMutableAttributedString = NSMutableAttributedString(string: "READ MORE", 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("moreStringRange\(moreStringRange)")

        tapRec.addTarget(self, action: "didTap:")
        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: &glyphRange)

        //Compute the rect of glyph in the text container
        print("glyphRange\(glyphRange)")
        print("textContainer\(textContainer)")
        let glyphRect:CGRect = layoutManager.boundingRectForGlyphRange(glyphRange, inTextContainer: textContainer)

        //Final rect relative to the textLabel.
        print("\(glyphRect)")

        //Now figure out if the touch point is inside our rect
        let touchPoint:CGPoint = tapRec.locationOfTouch(0, inView: lblTest)

        if CGRectContainsPoint(glyphRect, touchPoint) {
            print("User tapped on Read More. So show something more")
        }
    }

Es ist nur eine demo um zu testen, was ich tun will:

Swift : Tippen Sie auf einen Teil des Textes von UILabel

Jegliche Hilfe würde sehr geschätzt werden.

Hoffe, dies wird Ihnen helfen, Lesen Sie mehr
Wie wäre es mit zwei Bezeichnungen?
Es ist nur eine demo. Mein text hat viele Teile mit Anhängen. Ich kann nicht viele Labels.
Wie gesagt, es ist nur eine demo, die "Lesen Sie mehr" (kann auch etwas anderes sein), kann nicht in das Ende der Zeichenkette.
Dann prüfen Sie eine stackoverflow.com/questions/32175144/...

InformationsquelleAutor Ashley | 2016-03-16

Schreibe einen Kommentar