回答編集履歴

7

修正

2017/11/06 04:38

投稿

xAxis
xAxis

スコア1349

test CHANGED
@@ -110,7 +110,7 @@
110
110
 
111
111
 
112
112
 
113
- //UITextView.index(_,offsetBy:)のoffsetByは負の数と落ちる
113
+ //UITextView.index(_,offsetBy:)で得られるindexが負の数になると落ちる
114
114
 
115
115
  let targetOfLeftIndex = fullText.index(startIndex, offsetBy: offset - 1)
116
116
 

6

修正

2017/11/06 04:38

投稿

xAxis
xAxis

スコア1349

test CHANGED
@@ -70,15 +70,27 @@
70
70
 
71
71
 
72
72
 
73
- 勢いで全て書いてしまった。反省はしていない。
74
-
75
73
  ```swift
76
74
 
77
75
  @IBAction func deleteCharacterAtLeftOfCaret(_ sender: UIButton) {
78
76
 
77
+
78
+
79
-
79
+ //キャレットの位置
80
+
81
+      //文字列が"あいうえお"の時、キャレットが"あ"の左側にあるならばoffset == 0
80
82
 
81
83
  let offset = textView.selectedRange.location
84
+
85
+      
86
+
87
+      //キャレットが一番左にある場合削除する文字がないので抜ける
88
+
89
+ if offset <= 0 {
90
+
91
+ return
92
+
93
+ }
82
94
 
83
95
 
84
96
 
@@ -96,7 +108,9 @@
96
108
 
97
109
  let endIndex = fullText.endIndex
98
110
 
111
+
112
+
99
-
113
+ //UITextView.index(_,offsetBy:)のoffsetByは負の数だと落ちる
100
114
 
101
115
  let targetOfLeftIndex = fullText.index(startIndex, offsetBy: offset - 1)
102
116
 
@@ -116,6 +130,8 @@
116
130
 
117
131
  textView.text = String(modifiedText)
118
132
 
133
+
134
+
119
135
  }
120
136
 
121
137
  ```

5

修正

2017/11/05 04:38

投稿

xAxis
xAxis

スコア1349

test CHANGED
@@ -80,14 +80,6 @@
80
80
 
81
81
  let offset = textView.selectedRange.location
82
82
 
83
- //キャレットが一番左にある場合も抜ける
84
-
85
- if offset <= 0 {
86
-
87
- return
88
-
89
- }
90
-
91
83
 
92
84
 
93
85
       //テキストがない場合抜ける

4

修正

2017/11/04 03:49

投稿

xAxis
xAxis

スコア1349

test CHANGED
@@ -76,32 +76,54 @@
76
76
 
77
77
  @IBAction func deleteCharacterAtLeftOfCaret(_ sender: UIButton) {
78
78
 
79
- if let fullText = textView.text {
79
+
80
80
 
81
- let startIndex = fullText.startIndex
81
+ let offset = textView.selectedRange.location
82
82
 
83
- let endIndex = fullText.endIndex
83
+ //キャレットが一番左にある場合も抜ける
84
84
 
85
- let offset = textView.selectedRange.location
85
+ if offset <= 0 {
86
86
 
87
- let targetOfLeftIndex = fullText.index(startIndex, offsetBy: offset - 1)
88
-
89
- let targetOfRightIndex = fullText.index(startIndex, offsetBy: offset)
90
-
91
- let leftText = fullText[startIndex..<targetOfLeftIndex]
92
-
93
- let rightText = fullText[targetOfRightIndex..<endIndex]
94
-
95
- let modifiedText = leftText + rightText
96
-
97
- textView.text = String(modifiedText)
87
+ return
98
88
 
99
89
  }
90
+
91
+
92
+
93
+      //テキストがない場合抜ける
94
+
95
+ guard let fullText = textView.text else {
96
+
97
+ return
98
+
99
+ }
100
+
101
+
102
+
103
+ let startIndex = fullText.startIndex
104
+
105
+ let endIndex = fullText.endIndex
106
+
107
+
108
+
109
+ let targetOfLeftIndex = fullText.index(startIndex, offsetBy: offset - 1)
110
+
111
+ let targetOfRightIndex = fullText.index(startIndex, offsetBy: offset)
112
+
113
+
114
+
115
+ let leftText = fullText[startIndex..<targetOfLeftIndex]
116
+
117
+ let rightText = fullText[targetOfRightIndex..<endIndex]
118
+
119
+
120
+
121
+ let modifiedText = leftText + rightText
122
+
123
+
124
+
125
+ textView.text = String(modifiedText)
100
126
 
101
127
  }
102
128
 
103
129
  ```
104
-
105
-
106
-
107
- キャレットの左側の文字を削除したらキャレットが文字列の先頭に行っちゃうのでそれはいい塩梅にしてください。

3

追記の追記

2017/11/04 03:48

投稿

xAxis
xAxis

スコア1349

test CHANGED
@@ -63,3 +63,45 @@
63
63
 
64
64
 
65
65
  これならどうでしょうか?
66
+
67
+
68
+
69
+ ###追記の追記
70
+
71
+
72
+
73
+ 勢いで全て書いてしまった。反省はしていない。
74
+
75
+ ```swift
76
+
77
+ @IBAction func deleteCharacterAtLeftOfCaret(_ sender: UIButton) {
78
+
79
+ if let fullText = textView.text {
80
+
81
+ let startIndex = fullText.startIndex
82
+
83
+ let endIndex = fullText.endIndex
84
+
85
+ let offset = textView.selectedRange.location
86
+
87
+ let targetOfLeftIndex = fullText.index(startIndex, offsetBy: offset - 1)
88
+
89
+ let targetOfRightIndex = fullText.index(startIndex, offsetBy: offset)
90
+
91
+ let leftText = fullText[startIndex..<targetOfLeftIndex]
92
+
93
+ let rightText = fullText[targetOfRightIndex..<endIndex]
94
+
95
+ let modifiedText = leftText + rightText
96
+
97
+ textView.text = String(modifiedText)
98
+
99
+ }
100
+
101
+ }
102
+
103
+ ```
104
+
105
+
106
+
107
+ キャレットの左側の文字を削除したらキャレットが文字列の先頭に行っちゃうのでそれはいい塩梅にしてください。

2

修正

2017/11/03 07:58

投稿

xAxis
xAxis

スコア1349

test CHANGED
@@ -48,7 +48,7 @@
48
48
 
49
49
  ```swift
50
50
 
51
- @IBAction func delete(_ sender: UIButton) {
51
+ @IBAction func deleteText(_ sender: UIButton) {
52
52
 
53
53
  if let selectedRange = textView.selectedTextRange {
54
54
 

1

追記

2017/11/03 06:54

投稿

xAxis
xAxis

スコア1349

test CHANGED
@@ -35,3 +35,31 @@
35
35
  print(dropF). //abcdeghijklmn
36
36
 
37
37
  ```
38
+
39
+
40
+
41
+ ###以下追記
42
+
43
+
44
+
45
+ ネットやUITextViewのコードを眺めてたらUITextViewのrangeやpositionって扱いにくいですねこれ。なのでもう一つ思いつきました。
46
+
47
+
48
+
49
+ ```swift
50
+
51
+ @IBAction func delete(_ sender: UIButton) {
52
+
53
+ if let selectedRange = textView.selectedTextRange {
54
+
55
+ textView.replace(selectedRange, withText: "")
56
+
57
+ }
58
+
59
+ }
60
+
61
+ ```
62
+
63
+
64
+
65
+ これならどうでしょうか?