如何覆盖ReadMoreTextView pod的默认行为以不阻止链接检测



在我的项目中,我只是将ReadMoreTextView库与cocoapods一起使用。

这就是它的实现方式:

let textView: UITextView = {
let view = ReadMoreTextView(maximumNumberOfLines: 5)
view.textColor = .mineShaft
view.isScrollEnabled = false
view.isEditable = false
view.dataDetectorTypes = [.link]
view.textAlignment = .left
view.textContainerInset = .zero
view.textContainer.lineFragmentPadding = 0
return view
}()

带有简单扩展:

extension ReadMoreTextView {
convenience init(maximumNumberOfLines: Int) {
self.init()
self.maximumNumberOfLines = maximumNumberOfLines
shouldTrim = true
}
}

但这不起作用,也无法检测到链接。为什么?当我用UITextView替换ReadMoreTextView时,一切都正常。我能以某种方式覆盖此的默认行为吗?

对不起,我也研究了ReadMoreTextView库很长时间,然后我可以检测到链接,但没有点击事件响应。

我的代码如下:

let textView: ReadMoreTextView = {
let view = ReadMoreTextView(maximumNumberOfLines:100);
view.textColor = UIColor.red;
view.backgroundColor = UIColor.gray;
view.dataDetectorTypes = [.link]
view.isScrollEnabled = false
view.isEditable = true
view.textAlignment = .left
view.textContainerInset = .zero
view.textContainer.lineFragmentPadding = 0
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
textView.frame = CGRect(x: 20, y: 100, width: 300, height: 400);
textView.text  = "Lorem http://ipsum.com dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda,detect link will link string."
view.addSubview(textView)
textView.delegate = self;
let attStr : NSMutableAttributedString = textView.attributedText.mutableCopy() as! NSMutableAttributedString
print(attStr);
//        let attStr = NSMutableAttributedString(string: textView.text)
let content : String = textView.text
let linkStr : String = "will link string"
//        let linkStrOther : String = "will link  other string"
let toogleProtocol :String = "userProtocol://"

if (content as NSString).range(of: linkStr).location != NSNotFound {
attStr.addAttribute(NSAttributedString.Key.link, value: toogleProtocol, range: (content as NSString).range(of: linkStr))
}
print(attStr);
textView.attributedText = attStr
///Only one color can be set
textView.linkTextAttributes =  [
NSAttributedString.Key.foregroundColor: UIColor.blue
]
}
enum ClickLinkType {
case userProtocol
case privacyPolicy
}
var clickHandle:((_ clickType:ClickLinkType)->())?

func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
if URL.scheme  ==  "userProtocol"{
self.clickHandle?(.userProtocol)
return false
}else if URL.scheme == "privacyPolicy"{
self.clickHandle?(.privacyPolicy)
return false
}
return true
}

你可以看看ExpandableLabel库,它应该可以解决你的问题。

相关内容

  • 没有找到相关文章

最新更新