Skip to content

Instantly share code, notes, and snippets.

@agibson73
Last active March 3, 2016 16:00
Show Gist options
  • Save agibson73/a19f04259696feef5b29 to your computer and use it in GitHub Desktop.
Save agibson73/a19f04259696feef5b29 to your computer and use it in GitHub Desktop.
// when setting up your attributed String add an ATTRIBUTE to look for
// in this example button
// when i set up the string for the button I might use this
[NSForegroundColorAttributeName : UIColor.lightGrayColor(),NSFontAttributeName: font,"button":"button"]
func handleTapOnTextView(sender:UITextView){
let storedAttributedString = self.textView.attributedText
let textView = sender.view as! UITextView
let layoutManager = textView.layoutManager
let location = sender.locationInView(textView)
var characterIndex : Int!
characterIndex = layoutManager.characterIndexForPoint(location, inTextContainer: textView.textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
// adjust by 1 to make it not pick up clicks really inside the textview
if (characterIndex < textView.textStorage.length - 1) {
var testRange = NSRange(location:0,length: 0)
if let _ = textView.attributedText?.attribute("button", atIndex: characterIndex, effectiveRange: &testRange) as? NSString{
// use the range and change the color of text in the range
self.textView.attributedText = self.userCommmentsPressState(storedAttributedString, range: testRange, color: UIColor.darkGrayColor())
self.delay(0.23, closure: {
// attribute the label
UIView.animateWithDuration(0.3, animations: {
self.textView.attributedText = storedAttributedString
})
// do action whatever action you want
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment