Skip to content

Instantly share code, notes, and snippets.

self.textView.attributedText = attributedString
self.textView.font = UIFont.boldSystemFont(ofSize: 20)
self.textView.linkTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.systemBlue]
let attributedString = NSMutableAttributedString(string: text)
import UIKit
import SafariServices
class ViewController: UIViewController {
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
textView.delegate = self
// 引数 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)
}