2

I have a UILabel which contains some string as

"I agree to below Terms & Condistions"

.

Now on click of "Terms & conditions" i want to get it's frame so that i can add a button on that position at run time to detect the touch on particular word. By i am not sure how can i detect ?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
TechChain
  • 8,404
  • 29
  • 103
  • 228

1 Answers1

1

We can't make a uilabel accessible properties as you want in your case we can make use of TextView here for such property

my class :

import UIKit

class TextViewVC: UIViewController {

    @IBOutlet weak var textView: UITextView!

    let termsAndConditionsURL = "termsandconditions"

    override func viewDidLoad() {
        super.viewDidLoad()

        textView.delegate = self
        // Do any additional setup after loading the view.

        let str = "I agree to below Terms & Condistions"
        let attributedString = NSMutableAttributedString(string: str)
        let foundRange = attributedString.mutableString.range(of: "Terms & Condistions")
        attributedString.addAttribute(.foregroundColor, value: UIColor.blue, range: foundRange)
        attributedString.addAttribute(.underlineStyle , value: NSUnderlineStyle.styleSingle.rawValue, range: foundRange)
        attributedString.addAttribute(.link, value: termsAndConditionsURL, range: foundRange)

        textView.attributedText = attributedString

    }
}

extension TextViewVC : UITextViewDelegate {
    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool
    {
        if (URL.absoluteString == termsAndConditionsURL)
        {
            print("Need an action here")
        }
        else {
            print("No")
        }

        return false
    }
}

My storyBoard for creating a textView :

enter image description here

Simulator output

enter image description here

Console Output

enter image description here

iOS Geek
  • 4,825
  • 1
  • 9
  • 30
  • NSAttributedStringKey keyword is redundant. `attributedString.addAttribute(.foregroundColor, value: UIColor.blue, range: foundRange)`. The same applies for `.underlineStyle` and `.link`. Btw no need to include `didReceiveMemoryWarning` method in your code. – Leo Dabus Nov 20 '17 at 06:11
  • I haven't tested but `rawValue` might not be necessary also. It compiles without it I just don't know if it would run properly as I have seen cases where it doesn't. – Leo Dabus Nov 20 '17 at 06:15
  • it is running properly in my case as I checked I just created a sample file to check do this code is woking or not so I didn't removed didReceiveMemoryWarning that came up by default if you want I can share a link to my code file – iOS Geek Nov 20 '17 at 06:54
  • I mean NSAttributedStringKey is inferred by the compiler so you don't need to add it there. If you don't add any code in your didReceiveMemoryWarning method it is ok to remove it also. – Leo Dabus Nov 20 '17 at 06:57
  • The only think you should test is if `attributedString.addAttribute(.underlineStyle , value: NSUnderlineStyle.styleSingle, range: foundRange)` works (without the .rawValue) – Leo Dabus Nov 20 '17 at 06:59
  • Yes Thats why I told you to test it before removing it. I don't know why it doesn't work but it should work. You should consider reporting a bug to Apple – Leo Dabus Nov 20 '17 at 07:02