This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| self.textView.attributedText = attributedString | |
| self.textView.font = UIFont.boldSystemFont(ofSize: 20) | |
| self.textView.linkTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.systemBlue] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let attributedString = NSMutableAttributedString(string: text) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import UIKit | |
| import SafariServices | |
| class ViewController: UIViewController { | |
| @IBOutlet weak var textView: UITextView! | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| textView.delegate = self |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 引数 text : TextViewで表示するテキスト | |
| // 戻り値 [String] : 検知されたURL | |
| func detectUrls(text: String) -> [String] { | |
| guard let detector = try? NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue) else { | |
| return [] | |
| } | |
| let enableLinkTuples = detector.matches(in: text, range: NSRange(location: 0, length: text.count)) | |
| var detectedUrls = enableLinkTuples.map { checkingResult -> String in | |
| return (text as NSString).substring(with: checkingResult.range) | |
| } |