如何使tappable NSAttributedString移动到另一个ViewController Swift

问题描述 投票:-1回答:2

我正在研究一个简单的应用程序并以编程方式添加NSAttributedString。我真的很困惑如何将现在的viewcontroller移动到另一个viewcontroller

这不是重复的问题,因为我的问题是如何移动到查看控制器和duplicatie问题如何打开链接点击How can I make a clickable link in an 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)
}

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

任何帮助将不胜感激。

提前致谢。

ios swift uiviewcontroller nsattributedstring nsattributedstringkey
2个回答
2
投票

尝试使用此代码制作NSMutableAttributedString,您可以将其作为动态字符串值作为参数传递的动态。

把qazxsw poi而不是qazxsw poi

UItextView

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

UItextView

从故事板中设置以下属性。

UIviewController


0
投票

根据您的要求,您有两个选项可以轻松地使用// 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 } 库下的用户或将控件更改为enter image description here

我为UILabel课程找到了很棒的图书馆,这一定会对你有所帮助。我用这个做了同样的事情。

  1. 的UILabel UILabel
© www.soinside.com 2019 - 2024. All rights reserved.