質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

1回答

2580閲覧

TextView のリンクカスタマイズ

fooi

総合スコア9

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2017/06/15 13:37

編集2022/01/12 10:55

TwitterのようにハッシュタグをUITextViewでリンク化したいのでこのページを参考に、
https://stackoverflow.com/questions/34294064/how-to-make-uitextview-detect-hashtags
このコードを設定しました

swift

1UITextViewHashtagExtension.swift 2import UIKit 3extension UITextView { 4 func resolveTags(){ 5 let nsText:NSString = self.text as NSString! 6 7 // this needs to be an array of NSString. String does not work. 8 let words:[String] = nsText.components(separatedBy: " ") 9 10 // you can't set the font size in the storyboard anymore, since it gets overridden here. 11 let attrs = [ 12 NSFontAttributeName : UIFont.systemFont(ofSize: 17.0) 13 ] 14 15 // you can staple URLs onto attributed strings 16 let attrString = NSMutableAttributedString(string: nsText as String, attributes:attrs) 17 18 // tag each word if it has a hashtag 19 for word in words { 20 21 // found a word that is prepended by a hashtag! 22 // homework for you: implement @mentions here too. 23 if word.hasPrefix("#") { 24 25 // a range is the character position, followed by how many characters are in the word. 26 // we need this because we staple the "href" to this range. 27 let matchRange:NSRange = nsText.range(of: word as String) 28 29 // convert the word from NSString to String 30 // this allows us to call "dropFirst" to remove the hashtag 31 var stringifiedWord:String = word as String 32 33 // drop the hashtag 34 stringifiedWord = String(stringifiedWord.characters.dropFirst()) 35 36 // check to see if the hashtag has numbers. 37 // ribl is "#1" shouldn't be considered a hashtag. 38 let digits = NSCharacterSet.decimalDigits 39 40 if (stringifiedWord.rangeOfCharacter(from: digits) != nil) { 41 // hashtag contains a number, like "#1" 42 // so don't make it clickable 43 44 } else { 45 // set a link for when the user clicks on this word. 46 // it's not enough to use the word "hash", but you need the url scheme syntax "hash://" 47 // note: since it's a URL now, the color is set to the project's tint color 48 attrString.addAttribute(NSLinkAttributeName, value: "hash://" + stringifiedWord, range: matchRange) 49 } 50 } 51 52 self.attributedText = attrString 53 } 54 55} 56}

swift

1ViewController.swift 2class ViewController: UIViewController, UITextViewDelegate { 3 @IBOutlet weak var textView: UITextView! 4 override func viewDidLoad() { 5 super.viewDidLoad() 6 7 self.textView.text = "#テスト" 8 self.textView.resolveTags() 9 //self.textView.resolveAtTags() 10 textView.linkTextAttributes = [NSForegroundColorAttributeName:UIColor.red] 11 12 } 13 func textView(_ textView: UITextView, 14 shouldInteractWith URL: URL, 15 in characterRange: NSRange, 16 interaction: UITextItemInteraction) -> Bool { 17 18 print(URL) 19 20 return false 21 } 22 23}

実行し、ハッシュタグ部分をタップするとこのエラーが出てクラッシュしてしまいます
イメージ説明
正しく実行するにはどのようにすれば良いのでしょうか
ご教授をお願いいたします

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kei344

2017/06/16 08:11

回答が付いた質問の編集は慎重に行ってください。タイプミス程度なら修正する事もありますが、本文を削除するのはやめましょう。
PineMatsu

2017/06/16 08:13

回答が投稿された後に何で「あああ」の連続で書き換えるのですか?
guest

回答1

0

ハッシュタグ部分をタップすると、textView(_:shouldInteractWith:in:interaction:)が呼ばれますが、その際に日本語がそのまま渡されていたことが原因だと思われます。

UITextViewHashtagExtension.swiftでエンコードし、ViewController.swifttextView(_:shouldInteractWith:in:interaction:)でデコードしています。

swift

1// attrString.addAttribute(NSLinkAttributeName, value: "hash://" + stringifiedWord, range: matchRange) 2let encodedBody = stringifiedWord.addingPercentEncoding(withAllowedCharacters: .alphanumerics) 3attrString.addAttribute(NSLinkAttributeName, value: "hash://" + encodedBody!, range: matchRange)

swift

1 func textView(_ textView: UITextView, 2 shouldInteractWith URL: URL, 3 in characterRange: NSRange, 4 interaction: UITextItemInteraction) -> Bool { 5 6 print(URL) // hash://%E3%83%86%E3%82%B9%E3%83%88 7 8 let decodedString = URL.absoluteString.removingPercentEncoding 9 print(decodedString as Any) // Optional("hash://テスト") 10 11 return false 12 } 13

投稿2017/06/15 23:52

izkn

総合スコア1698

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問