質問編集履歴

1

シンプルに変更

2020/11/23 08:50

投稿

Hayato1201
Hayato1201

スコア220

test CHANGED
@@ -1 +1 @@
1
- SwiftUIでUITextViewをラップして使った場合自動改行ができないです。
1
+ SwiftUIでUITextViewをラップして使った場合改行につです。
test CHANGED
@@ -1,4 +1,4 @@
1
- 以下の様にUIViewRepresentableを使ってUITextViewをラップしてSwiftUI上で使える様にしたのですが、枠できて折り返てくれません。。
1
+ 以下の様にUIViewRepresentableを使ってUITextViewをラップしてSwiftUI上で使える様にしまし
2
2
 
3
3
 
4
4
 
@@ -6,39 +6,7 @@
6
6
 
7
7
  struct CustomTextView: UIViewRepresentable {
8
8
 
9
-
10
-
11
- class Coordinator: NSObject, UITextViewDelegate {
12
-
13
- @Binding var text: String
14
-
15
-
16
-
17
- init(text: Binding<String>) {
18
-
19
- _text = text
20
-
21
- }
22
-
23
9
 
24
-
25
- func textViewDidChangeSelection(_ textView: UITextView) {
26
-
27
- text = textView.text ?? ""
28
-
29
- }
30
-
31
- }
32
-
33
-
34
-
35
-
36
-
37
- @Binding var text: String
38
-
39
-
40
-
41
-
42
10
 
43
11
  func makeUIView(context: UIViewRepresentableContext<CustomTextView>) -> UITextView {
44
12
 
@@ -52,27 +20,37 @@
52
20
 
53
21
  textView.font = UIFont(name: "ArialMT", size: 20)
54
22
 
55
- textView.delegate = context.coordinator
23
+ // textView.text = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
56
-
57
-
58
24
 
59
25
  return textView
60
26
 
61
27
  }
62
28
 
63
- func makeCoordinator() -> CustomTextView.Coordinator {
29
+
64
30
 
65
- return Coordinator(text: $text)
66
-
67
- }
68
-
69
- func updateUIView(_ uiView: UITextView, context: UIViewRepresentableContext<CustomTextView>) {
31
+ func updateUIView(_ uiView: UITextView, context: Context) {
70
32
 
71
33
 
72
34
 
73
35
  }
74
36
 
75
37
  }
38
+
39
+
40
+
41
+
42
+
43
+ extension UIApplication {
44
+
45
+ func closeKeyboard() {
46
+
47
+ sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
48
+
49
+ }
50
+
51
+ }
52
+
53
+
76
54
 
77
55
  ```
78
56
 
@@ -86,11 +64,13 @@
86
64
 
87
65
  ```SwiftUI
88
66
 
67
+ import SwiftUI
68
+
69
+
70
+
89
71
  struct ContentView: View {
90
72
 
91
- @State private var str = ""
73
+
92
-
93
-
94
74
 
95
75
  var body: some View {
96
76
 
@@ -98,9 +78,7 @@
98
78
 
99
79
  ZStack {
100
80
 
101
-
102
-
103
- CustomTextView(text: $str)
81
+ CustomTextView()
104
82
 
105
83
  .frame(width:300, height: 300, alignment: .topLeading)
106
84
 
@@ -124,14 +102,22 @@
124
102
 
125
103
 
126
104
 
127
- この様に改行されずに右に行ってしまいます。
128
-
129
- ![イメジ説明](54e868e91da4ec3714df5115138070af.png)
105
+ これでキボードから値を入力した場合、以下の様に赤枠のframeで折り返してくれます。
130
106
 
131
107
 
132
108
 
109
+ ![イメージ説明](5651ea7a5926799c6f63e2c595c64046.png)
133
110
 
134
111
 
112
+
135
- どうすれば文字が赤枠のCustomTextViewの端まできたら改行される様にできるでょうか?
113
+ しかし上記CustomTextViewのコメントアウトを外して直接textView.textに文字列を入れると以下の様に枠をはみ出てしまいます。
114
+
115
+ ![![イメージ説明](88d40ed914a5bfce44cc239375c3fe58.png)
116
+
117
+
118
+
119
+ 直接文字を入力した場合も枠で改行される様にしたいです。
120
+
121
+
136
122
 
137
123
  分かる方、ご回答いただければ幸いです。