1

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.

Sham Dhiman
  • 1,348
  • 1
  • 21
  • 59
  • Apple Documentation on `NSAttributedString.Key.link`: The value of this attribute is an NSURL object (preferred) or an NSString object. The default value of this property is nil, indicating no link. – wzso Feb 18 '19 at 07:47
  • Possible duplicate of [How can I make a clickable link in an NSAttributedString?](https://stackoverflow.com/questions/21629784/how-can-i-make-a-clickable-link-in-an-nsattributedstring) – wzso Feb 18 '19 at 07:48
  • @0xa6a yes i know very well but please tell me how to view controller move click on attributeString – Sham Dhiman Feb 18 '19 at 07:56
  • @0xa6a this is for a link Open not for ViewController i am just trying to use this but not give any solution – Sham Dhiman Feb 18 '19 at 07:57

2 Answers2

7

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 = "dhiman@gmail.com 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

AtulParmar
  • 4,358
  • 1
  • 24
  • 45
  • Atul Sir please explain this line: formattedString.addAttributes(formattedStringAttribute, range: NSMakeRange(0, formattedString.length)) and also not here any name of valriable `formattedStringAttribute` – Sham Dhiman Feb 18 '19 at 10:07
0

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]

piet.t
  • 11,718
  • 21
  • 43
  • 52