你需要添加
myTextView.linkTextAttributes
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
let attributes = [NSAttributedStringKey.foregroundColor : UIColor.init(rgb: 0x646464),
NSAttributedStringKey.font : AppFont.getFont(fontType: .regular, ofSize: 16),
NSAttributedStringKey.paragraphStyle : paragraphStyle]
let attributedText = NSMutableAttributedString(string: descriptonText,
attributes: attributes)
if let range = attributedText.string.range(of: "terms & conditions") {
let nsRange = NSRange(range, in: attributedText.string)
attributedText.addAttributes([NSAttributedStringKey.link : "link://T&C"], range: nsRange)
}
let linkColor = UIColor.init(red: 47, green: 117, blue: 83)
let linkAttrs: [String: Any] = [NSAttributedStringKey.foregroundColor.rawValue : linkColor,
NSAttributedStringKey.underlineColor.rawValue : linkColor,
NSAttributedStringKey.underlineStyle.rawValue : NSUnderlineStyle.styleSingle.rawValue]
descriptionTextView.linkTextAttributes = linkAttrs
let textView = UITextView(frame: descriptionTextView.bounds)
textView.attributedText = attributedText
textView.layoutIfNeeded()
textView.sizeToFit()
let size = textView.sizeThatFits(CGSize(width: textView.frame.size.width, height: CGFloat.greatestFiniteMagnitude))
descriptionTextViewHeight.constant = size.height
descriptionTextView.attributedText = attributedText
func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
return false
}
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
return false
}
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
debugPrint(URL.absoluteString)
if URL.absoluteString.contains("link://") {
// my func call
dismissAnimated()
}
return false
}
同时将isEditable设置为false,将isSelectable设置为true。子类textView并在中返回false
canPerformAction
. 在
awakeFromNib
设置
textDragInteraction?.isEnabled = false