質問編集履歴

2

質問が不正に削除されていたため復旧を行いました

2017/06/19 00:08

投稿

fooi
fooi

スコア9

test CHANGED
File without changes
test CHANGED
@@ -1 +1,179 @@
1
- ああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああ
1
+ TwitterのようにハッシュタグをUITextViewでリンク化したいのでこのページを参考に、
2
+
3
+ https://stackoverflow.com/questions/34294064/how-to-make-uitextview-detect-hashtags
4
+
5
+ このコードを設定しました
6
+
7
+ ```swift
8
+
9
+ UITextViewHashtagExtension.swift
10
+
11
+ import UIKit
12
+
13
+ extension UITextView {
14
+
15
+ func resolveTags(){
16
+
17
+ let nsText:NSString = self.text as NSString!
18
+
19
+
20
+
21
+ // this needs to be an array of NSString. String does not work.
22
+
23
+ let words:[String] = nsText.components(separatedBy: " ")
24
+
25
+
26
+
27
+ // you can't set the font size in the storyboard anymore, since it gets overridden here.
28
+
29
+ let attrs = [
30
+
31
+ NSFontAttributeName : UIFont.systemFont(ofSize: 17.0)
32
+
33
+ ]
34
+
35
+
36
+
37
+ // you can staple URLs onto attributed strings
38
+
39
+ let attrString = NSMutableAttributedString(string: nsText as String, attributes:attrs)
40
+
41
+
42
+
43
+ // tag each word if it has a hashtag
44
+
45
+ for word in words {
46
+
47
+
48
+
49
+ // found a word that is prepended by a hashtag!
50
+
51
+ // homework for you: implement @mentions here too.
52
+
53
+ if word.hasPrefix("#") {
54
+
55
+
56
+
57
+ // a range is the character position, followed by how many characters are in the word.
58
+
59
+ // we need this because we staple the "href" to this range.
60
+
61
+ let matchRange:NSRange = nsText.range(of: word as String)
62
+
63
+
64
+
65
+ // convert the word from NSString to String
66
+
67
+ // this allows us to call "dropFirst" to remove the hashtag
68
+
69
+ var stringifiedWord:String = word as String
70
+
71
+
72
+
73
+ // drop the hashtag
74
+
75
+ stringifiedWord = String(stringifiedWord.characters.dropFirst())
76
+
77
+
78
+
79
+ // check to see if the hashtag has numbers.
80
+
81
+ // ribl is "#1" shouldn't be considered a hashtag.
82
+
83
+ let digits = NSCharacterSet.decimalDigits
84
+
85
+
86
+
87
+ if (stringifiedWord.rangeOfCharacter(from: digits) != nil) {
88
+
89
+ // hashtag contains a number, like "#1"
90
+
91
+ // so don't make it clickable
92
+
93
+
94
+
95
+ } else {
96
+
97
+ // set a link for when the user clicks on this word.
98
+
99
+ // it's not enough to use the word "hash", but you need the url scheme syntax "hash://"
100
+
101
+ // note: since it's a URL now, the color is set to the project's tint color
102
+
103
+ attrString.addAttribute(NSLinkAttributeName, value: "hash://" + stringifiedWord, range: matchRange)
104
+
105
+ }
106
+
107
+ }
108
+
109
+
110
+
111
+ self.attributedText = attrString
112
+
113
+ }
114
+
115
+
116
+
117
+ }
118
+
119
+ }
120
+
121
+ ```
122
+
123
+ ```swift
124
+
125
+ ViewController.swift
126
+
127
+ class ViewController: UIViewController, UITextViewDelegate {
128
+
129
+ @IBOutlet weak var textView: UITextView!
130
+
131
+ override func viewDidLoad() {
132
+
133
+ super.viewDidLoad()
134
+
135
+
136
+
137
+ self.textView.text = "#テスト"
138
+
139
+ self.textView.resolveTags()
140
+
141
+ //self.textView.resolveAtTags()
142
+
143
+ textView.linkTextAttributes = [NSForegroundColorAttributeName:UIColor.red]
144
+
145
+
146
+
147
+ }
148
+
149
+ func textView(_ textView: UITextView,
150
+
151
+ shouldInteractWith URL: URL,
152
+
153
+ in characterRange: NSRange,
154
+
155
+ interaction: UITextItemInteraction) -> Bool {
156
+
157
+
158
+
159
+ print(URL)
160
+
161
+
162
+
163
+ return false
164
+
165
+ }
166
+
167
+
168
+
169
+ }
170
+
171
+ ```
172
+
173
+ 実行し、ハッシュタグ部分をタップするとこのエラーが出てクラッシュしてしまいます
174
+
175
+ ![イメージ説明](f31fa734b8a73871ea6a08234861beeb.png)
176
+
177
+ 正しく実行するにはどのようにすれば良いのでしょうか
178
+
179
+ ご教授をお願いいたします

1

2017/06/19 00:07

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,189 +1 @@
1
- TwitterのようにハッシュタグをUITextViewでリンク化したいのでこのページを参考に、
2
-
3
- https://stackoverflow.com/questions/34294064/how-to-make-uitextview-detect-hashtags
4
-
5
- このコードを設定しました
6
-
7
- ```swift
8
-
9
- UITextViewHashtagExtension.swift
10
-
11
- import UIKit
12
-
13
- extension UITextView {
14
-
15
- func resolveTags(){
16
-
17
- let nsText:NSString = self.text as NSString!
18
-
19
-
20
-
21
- // this needs to be an array of NSString. String does not work.
22
-
23
- let words:[String] = nsText.components(separatedBy: " ")
24
-
25
-
26
-
27
- // you can't set the font size in the storyboard anymore, since it gets overridden here.
28
-
29
- let attrs = [
30
-
31
- NSFontAttributeName : UIFont.systemFont(ofSize: 17.0)
32
-
33
- ]
34
-
35
-
36
-
37
- // you can staple URLs onto attributed strings
38
-
39
- let attrString = NSMutableAttributedString(string: nsText as String, attributes:attrs)
40
-
41
-
42
-
43
- // tag each word if it has a hashtag
44
-
45
- for word in words {
46
-
47
-
48
-
49
- // found a word that is prepended by a hashtag!
50
-
51
- // homework for you: implement @mentions here too.
52
-
53
- if word.hasPrefix("#") {
54
-
55
-
56
-
57
- // a range is the character position, followed by how many characters are in the word.
58
-
59
- // we need this because we staple the "href" to this range.
60
-
61
- let matchRange:NSRange = nsText.range(of: word as String)
62
-
63
-
64
-
65
- // convert the word from NSString to String
66
-
67
- // this allows us to call "dropFirst" to remove the hashtag
68
-
69
- var stringifiedWord:String = word as String
70
-
71
-
72
-
73
- // drop the hashtag
74
-
75
- stringifiedWord = String(stringifiedWord.characters.dropFirst())
76
-
77
-
78
-
79
- // check to see if the hashtag has numbers.
80
-
81
- // ribl is "#1" shouldn't be considered a hashtag.
82
-
83
- let digits = NSCharacterSet.decimalDigits
84
-
85
-
86
-
87
- if (stringifiedWord.rangeOfCharacter(from: digits) != nil) {
88
-
89
- // hashtag contains a number, like "#1"
90
-
91
- // so don't make it clickable
92
-
93
-
94
-
95
- } else {
96
-
97
- // set a link for when the user clicks on this word.
98
-
99
- // it's not enough to use the word "hash", but you need the url scheme syntax "hash://"
100
-
101
- // note: since it's a URL now, the color is set to the project's tint color
102
-
103
- attrString.addAttribute(NSLinkAttributeName, value: "hash://" + stringifiedWord, range: matchRange)
104
-
105
- }
106
-
107
- }
108
-
109
-
110
-
111
- self.attributedText = attrString
112
-
113
- }
114
-
115
-
116
-
117
-
118
-
119
- }
120
-
121
- }
122
-
123
- ```
124
-
125
-
126
-
127
- ```swift
128
-
129
- ViewController.swift
130
-
131
- class ViewController: UIViewController, UITextViewDelegate {
132
-
133
-
134
-
135
- @IBOutlet weak var textView: UITextView!
136
-
137
- override func viewDidLoad() {
138
-
139
- super.viewDidLoad()
140
-
141
-
142
-
143
- self.textView.text = "#テスト"
144
-
145
- self.textView.resolveTags()
146
-
147
- //self.textView.resolveAtTags()
148
-
149
- textView.linkTextAttributes = [NSForegroundColorAttributeName:UIColor.red]
150
-
151
-
152
-
153
- }
154
-
155
-
156
-
157
- func textView(_ textView: UITextView,
158
-
159
- shouldInteractWith URL: URL,
160
-
161
- in characterRange: NSRange,
162
-
163
- interaction: UITextItemInteraction) -> Bool {
164
-
165
-
166
-
167
- print(URL)
168
-
169
-
170
-
171
- return false
172
-
173
- }
174
-
175
-
176
-
177
- }
178
-
179
- ```
180
-
181
- 実行し、ハッシュタグ部分をタップするとこのエラーが出てクラッシュしてしまいます
182
-
183
- ![イメージ説明](f31fa734b8a73871ea6a08234861beeb.png)
184
-
185
-
186
-
187
- 正しく実行するにはどのようにすれば良いのでしょうか
188
-
189
- ご教授をお願いいたします
1
+ ああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああ