如何使可点击的 NSAttributedString 移动到另一个 ViewController Swift

Sam*_*Sam 2 uiviewcontroller nsattributedstring ios swift nsattributedstringkey

我正在开发一个简单的应用程序并以编程方式添加NSAttributedString. 我真的很困惑如何将礼物viewcontroller转移到另一个viewcontroller

这不是重复的问题,因为我的问题是如何移动到视图控制器和重复问题如何在单击时打开链接 如何在 NSAttributedString 中创建可点击的链接?.

截屏

截屏

在上图中显示了三个,NSAttributedString第一个是dhiman.sham1gmail.com,第二个是has started following,第三个Sham。尝试单击时Sham不执行任何操作。我想在单击时将此控制器移动到另一个控制器Sham

这是我的代码:

@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)
}
Run Code Online (Sandbox Code Playgroud)

有人可以向我解释如何解决这个问题,我已经尝试解决这个问题,但还没有结果。

任何帮助将不胜感激。

提前致谢。

Atu*_*mar 6

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
    }
Run Code Online (Sandbox Code Playgroud)

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
    }
Run Code Online (Sandbox Code Playgroud)

Set following property from the storyboard.

在此处输入图片说明