質問編集履歴

1

コードの追加

2020/11/27 07:35

投稿

downtowntown
downtowntown

スコア21

test CHANGED
File without changes
test CHANGED
@@ -9,3 +9,67 @@
9
9
 
10
10
 
11
11
  [参考にした記事](https://riptutorial.com/ja/ios/example/13697/%E3%82%AB%E3%83%BC%E3%82%BD%E3%83%AB%E4%BD%8D%E7%BD%AE%E3%81%AE%E5%8F%96%E5%BE%97%E3%81%A8%E8%A8%AD%E5%AE%9A)
12
+
13
+
14
+
15
+ DidBeginEditing内の出力では0が表示されるのですが、カーソルは移動してなかったです。
16
+
17
+
18
+
19
+ ```swift
20
+
21
+ func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
22
+
23
+ if let selectedRange = textView.selectedTextRange {
24
+
25
+
26
+
27
+ textView.selectedRange.location = 0
28
+
29
+
30
+
31
+ let cursorPosition = textView.offset(from: textView.beginningOfDocument, to: selectedRange.start)
32
+
33
+
34
+
35
+ print(cursorPosition)
36
+
37
+
38
+
39
+ }
40
+
41
+
42
+
43
+ return true
44
+
45
+ }
46
+
47
+
48
+
49
+ func textViewDidBeginEditing(_ textView: UITextView) {
50
+
51
+
52
+
53
+ if let selectedRange = textView.selectedTextRange {
54
+
55
+
56
+
57
+ textView.selectedRange.location = 0
58
+
59
+
60
+
61
+ let cursorPosition = textView.offset(from: textView.beginningOfDocument, to: selectedRange.start)
62
+
63
+
64
+
65
+ print(cursorPosition)
66
+
67
+
68
+
69
+ }
70
+
71
+
72
+
73
+ }
74
+
75
+ ```