I have a text in my app when the user wants to login like “If you click on login you agree with our data privacy etc.”
The user can then click on the word data privacy and a view will open. The issue is that the tap on data privacy does not get recognized everytime I click on it. Only every 3 or 4th try. What could be the reason for that?
func showTermText(){
// Enable user interaction for the label
textForTerms.isUserInteractionEnabled = true
// Add a tap gesture recognizer
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
textForTerms.addGestureRecognizer(tapGesture)
// Set the attributed text with the clickable words (colors changed to red and green)
let attributedString = NSMutableAttributedString(string: "Mit Klick auf Login stimmst du unseren allgemeinen Geschäftsbedingungen und Datenschutzbedingungen zu.")
let clickableRange1 = (attributedString.string as NSString).range(of: "Geschäftsbedingungen", options: .caseInsensitive)
attributedString.addAttributes([.underlineStyle: NSUnderlineStyle.single.rawValue, .foregroundColor: UIColor.red], range: clickableRange1)
let clickableRange2 = (attributedString.string as NSString).range(of: "Datenschutzbedingungen", options: .caseInsensitive)
attributedString.addAttributes([.underlineStyle: NSUnderlineStyle.single.rawValue, .foregroundColor: UIColor.green], range: clickableRange2)
textForTerms.attributedText = attributedString
}
@objc func handleTap(_ gesture: UITapGestureRecognizer) {
guard let attributedText = textForTerms.attributedText else { return }
let layoutManager = NSLayoutManager()
let textContainer = NSTextContainer(size: textForTerms.bounds.size)
let textStorage = NSTextStorage(attributedString: attributedText)
layoutManager.addTextContainer(textContainer)
textStorage.addLayoutManager(layoutManager)
let location = gesture.location(in: textForTerms)
let characterIndex = layoutManager.characterIndex(for: location, in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
print("Character Index: \(characterIndex), Location: \(location)")
// Check if the tapped word is one of the clickable words
let clickableRange1 = (attributedText.string as NSString).range(of: "Geschäftsbedingungen")
if NSLocationInRange(characterIndex, clickableRange1) {
// Perform the action for Geschäftsbedingungen
print("Geschäftsbedingungen was tapped!")
}
let clickableRange2 = (attributedText.string as NSString).range(of: "Datenschutzbedingungen")
if NSLocationInRange(characterIndex, clickableRange2) {
// Perform the action for Datenschutzbedingungen
print("Datenschutzbedingungen was tapped!")
}
}
I also post you the answer when I tap on the words and this gets executed:
print("Character Index: \(characterIndex), Location: \(location)")
Character Index: 101, Location: (126.0, 32.666656494140625)
Character Index: 71, Location: (130.66665649414062, 22.666656494140625)
Character Index: 101, Location: (113.33332824707031, 30.333328247070312)
Character Index: 99, Location: (296.6666564941406, 26.666656494140625)
Character Index: 101, Location: (200.0, 28.0)
Character Index: 83, Location: (203.3333282470703, 25.666656494140625)
Datenschutzbedingungen was tapped!
Character Index: 52, Location: (14.666656494140625, 25.333328247070312)
Geschäftsbedingungen was tapped!
Character Index: 101, Location: (102.0, 35.0)




