UITextViewDelegate总结

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
extension ViewController: UITextViewDelegate{
/*
几乎所有操作都会触发textViewDidChangeSelection,包括点击文本框、增加内容删除内容
执行率高于 textViewDidBeginEditing 代理 也高于 UIKeyboardWillShowNotification 通知
*/
func textViewDidChangeSelection(_ textView: UITextView) {
print("\t textViewDidChangeSelection 几乎所有操作都会触发")
}
///是否可以开始编辑
func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
print("\t textViewShouldBeginEditing 是否可以开始编辑")
return true
}
///是否可以结束编辑
func textViewShouldEndEditing(_ textView: UITextView) -> Bool {
print("\t textViewShouldEndEditing 是否可以结束编辑")
return true
}
/// 开始编辑
func textViewDidBeginEditing(_ textView: UITextView) {
print("\t textViewDidBeginEditing 开始编辑")
}
/// 结束编辑
func textViewDidEndEditing(_ textView: UITextView) {
print("\t textViewDidEndEditing 结束编辑")
}
/// 编辑过程中触发
/// 只有在内容改变时才触发,而且这个改变内容是手动输入有效
func textViewDidChange(_ textView: UITextView) {
print("\t textViewDidChange 编辑过程中触发")
}
/// 编辑过程中触发,是否将 变更的内容 写入textView
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
print("\t 变更范围 \(range.length) \(range.location) - 变更内容 \(text)")
return true
}

func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
print("\t shouldInteractWith \(URL)")
return true
}
func textView(_ textView: UITextView, shouldInteractWith textAttachment: NSTextAttachment, in characterRange: NSRange) -> Bool {
print("\t shouldInteractWith \(textAttachment)")
return true
}

@available(iOS 10.0, *)
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
print("\t shouldInteractWith \(URL)")
return true
}
@available(iOS 10.0, *)
func textView(_ textView: UITextView, shouldInteractWith textAttachment: NSTextAttachment, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
print("\t shouldInteractWith \(textAttachment)")
return true
}
}

本文标题:UITextViewDelegate总结

文章作者:史彦超

发布时间:2016年10月11日 - 22:10

最后更新:2021年07月20日 - 16:07

原始链接:https://doingself.github.io/2016/10/11/2016-10-11-UITextViewDelegate%E6%80%BB%E7%BB%93/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

Donate comment here