How to make tappable NSAttributedString Move To Another ViewController Swift

4.2k Views Asked by At

I'm working on a simple app and add programatically NSAttributedString. I am really confused how to move present viewcontroller to another viewcontroller.

This is not duplicate question because my question for how move to view controller and duplicatie question how to open link on click How can I make a clickable link in an NSAttributedString?.

Screen shot

Screen Shot

In above image show three NSAttributedString first one is dhiman.sham1gmail.com and second one ishas started following and third one Sham. When trying to click on Sham no action perform. i want to move this controller to another controller when click on Sham .

This is my Code:

@IBOutlet weak var descriptionLbl: UILabel!

override func updateWithModel(_ model: AnyObject) {
 self.descriptionLbl.attributedText = self.followingGetTextFromType(data: (model as? FollowingNotificationData)!)
}

func followingGetTextFromType(data:FollowingNotificationData) -> NSMutableAttributedString{
  var attributedString = NSMutableAttributedString()
  attributedString = NSMutableAttributedString(string:(data.detailsNoti?.fullName)!, attributes: strres)
  let startedfollowing = NSMutableAttributedString(string:" has started following ", attributes: attrs)
  attributedString.append(startedfollowing)
  var discription = NSMutableAttributedString()
  if data.fullName == ""{
  discription = NSMutableAttributedString(string:"\((data.userName)!)", attributes: strres)
  }else{
  discription = NSMutableAttributedString(string:"\((data.fullName)!)", attributes: strres)
  }
  attributedString.append(discription)
}

Can someone please explain to me how to solve this , i've tried to solve this issue but no results yet.

Any help would be greatly appreciated.

Thanks in advance.

2

There are 2 best solutions below

1
AtulParmar On BEST ANSWER

Try this code to make NSMutableAttributedString, You can make it dynamic as your requirement to pass dynamic string value as a parameter.

Put UItextView insted of UIlabel

@IBOutlet weak var descriptionLbl: UITextView!

descriptionLbl.attributedText = prepareAttributedTextString()

func prepareAttributedTextString() -> NSMutableAttributedString {

        let masterString = "[email protected] has started following Sham"

        let formattedString = NSMutableAttributedString(string: masterString)

        let ShamUseAttribute = [
            NSAttributedStringKey.underlineStyle: NSUnderlineStyle.styleSingle.rawValue,
            NSAttributedStringKey.font: UIFont.systemFont(ofSize: 16.0),
            NSAttributedStringKey.link: URL(string: "Sham")!,
            ] as [NSAttributedStringKey : Any]


        let ShamRange = (masterString as NSString).range(of: "Sham")
        formattedString.addAttributes(ShamUseAttribute, range: ShamRange)

        return formattedString
    }

Use this UITextViewDelegate to detect tap on UItextView and predict to specified UIviewController

// MARK: UITextView Delegate Method
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {

       if (URL.absoluteString == "Sham") { 
             let objVC = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "YourViewController") as! YourViewController           
             self.navigationController?.pushViewController(objVC, animated: true)
        }
        return true
    }

Set following property from the storyboard.

enter image description here

2
AudioBubble On

You have two option to make things work as per your requirements easily either User below UILabel library or change your control to UITextView.

I found great library for UILabel class, this will surely help you. I did this same thing by using this.

  1. UILabel

    [TTTAttributedLabel]