繁体   English   中英

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

[英]How to make tappable NSAttributedString Move To Another ViewController Swift

我正在开发一个简单的应用程序,并以编程NSAttributedString添加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)
}

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

任何帮助将不胜感激。

提前致谢。

尝试使用以下代码使NSMutableAttributedString成为动态对象,您可以根据需要将动态字符串值作为参数传递。

UItextView 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
    }

使用此UITextViewDelegate来检测对UItextView点击并预测到指定的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
    }

在情节提要中设置以下属性。

在此处输入图片说明

您可以通过两种方法使UILabel库下面的User或将控件更改为UITextView从而轻松地根据您的要求进行操作。

我为UILabel类找到了不错的库,这一定会对您有所帮助。 我通过使用这个来做同样的事情。

  1. UILabel

    [TTTAttributedLabel]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM