-1

I need to make "Terms of Use" in the string below blue and have it respond to an click. I don't want to use a button so how can I do this in a UILabel? String: By tapping Next and continuing you agree to the Terms of Use

Code I tried but failed just got text color change:

var myMutableString = NSMutableAttributedString()
            myMutableString = NSMutableAttributedString(string: self.termsLabel.text! as String, attributes: [NSFontAttributeName:UIFont(name: self.termsLabel.font.fontName, size: self.termsLabel.font.pointSize)!])
            myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blue, range: NSRange(location:self.termsLabel.text!.characters.count - 12,length:12))
            self.termsLabel.attributedText = myMutableString
user1079052
  • 3,803
  • 4
  • 30
  • 55
  • It will help to know WHY it can't be a UIButton. There are some options to do this, but they may to be what you want. For instance, what is the backgroundColor of the UILabel? And again, why is this requirement not filled by the things you get with a UIContol or it's subclass, UIButton? –  Jan 03 '17 at 15:04
  • Is [this](http://stackoverflow.com/questions/32309247/add-read-more-to-the-end-of-uilabel) similar to what are you looking for? – Ahmad F Jan 03 '17 at 15:05
  • Thanks for answering for the OP. If that's the case, the answer is correct. But maybe there's more to this question than we know. Don't assume! My instincts **say** a UIButton works perfectly well, but the OP, with some rep BTW, may have a different reason than me and you assume. Thus, not **yet** obvious. –  Jan 03 '17 at 15:13
  • Whoa. I'm smelling a miscommunication happening. @user1079052, apologies, but I'm flagging this for clarity since you haven't responded yet. We need to know more - why you can use UIButton, what behavior you need, what you've tried, and what behavior you have seen. –  Jan 03 '17 at 15:25
  • wow, show down. I only want the text in blue to be clickable so a button won't help me with this – user1079052 Jan 03 '17 at 17:04
  • In that case, a UIButton **will** help you with this. Remember, by default a UIButton (iOS 7+) appears as blue text. There's no need to re-invent the wheel. Thanks for clearing things up. –  Jan 03 '17 at 18:22

2 Answers2

1

Don't use a UILabel. Use a UITextField or UITextView, and install attributed text into it, including a link. Then set up the delegate methods to detect & respond to links. You need to implement the UITextViewDelegate method

func textView(UITextView, 
  shouldInteractWith: URL, 
  in: NSRange, 
  interaction: UITextItemInteraction)

One of the easiest ways to configure your link is to load the contents of an RTF file into the field.

I have a demo project on GitHub called DatesInSwift that creates clickable links that trigger a custom URL that invokes the URL in the app.

That project uses an extension to UITextView that has adds @IBInspectable property RTF_Filename to UITextView fields. All you have to do is to set the RTF_Filename property of your text view to the filename of the RTF file that you need to load.

It looks like my project was written for Swift 2, and uses the old version of the UITextViewDelegate method, which was called

func textView(_ textView: UITextView, shouldInteractWithURL URL: Foundation.URL, inRange characterRange: NSRange) -> Bool

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • there isn't a a URL on the back of the clickable text. it is just an ibaction that has a seague to the next view. – user1079052 Jan 03 '17 at 17:05
  • If you want to have the text be clickable, rather than a transparent button that you manually place on top of the text, then you need to have a URL. If you look at the app I posted a link to, it uses a custom URL scheme to invoke a method in the app delegate when you click a link, and I pass that link to my view controller. – Duncan C Jan 03 '17 at 18:14
  • If you use a transparent button then placing the button on top of the text is a challenge, especially with different screen sizes, and if you support localization to different languages it gets REALLY hard. – Duncan C Jan 03 '17 at 18:15
  • If it's ok to have the user click anywhere on the entire text view (not just the highlighted text) then using a transparent button is the way to go. – Duncan C Jan 03 '17 at 18:16
  • Download the DatesInSwift app and try it out. (link in my answer.) It uses a number of clickable links that trigger code in the program. That can be a model for what you want to do. – Duncan C Jan 03 '17 at 20:12
0

I came to the conclusion that i couldn't get exactly what I wanted so I threw the text in a button and added this code

let mainText = "By tapping Next and continuing you agree to the Terms Of Use"
    let attributeText = "Terms Of Use"
    let range = (mainText as NSString).range(of: attributeText)
    let attributedString = NSMutableAttributedString(string:mainText)
    attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blue, range: range)
    self.termsLabel.attributedText = attributedString
    let tapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.goToTOSView))
    tapGesture.numberOfTapsRequired = 1
    self.termsLabel.isUserInteractionEnabled =  true
    self.termsLabel.addGestureRecognizer(tapGesture)
user1079052
  • 3,803
  • 4
  • 30
  • 55