From 8eb24871c5d5b6c6cf2bd290652240a017903e00 Mon Sep 17 00:00:00 2001 From: CMK Date: Mon, 15 Mar 2021 13:44:42 +0800 Subject: [PATCH] feat: add URL highlight for text editor --- .../Scene/Compose/ComposeViewController.swift | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Mastodon/Scene/Compose/ComposeViewController.swift b/Mastodon/Scene/Compose/ComposeViewController.swift index 729c81e8..8504ffc5 100644 --- a/Mastodon/Scene/Compose/ComposeViewController.swift +++ b/Mastodon/Scene/Compose/ComposeViewController.swift @@ -235,6 +235,8 @@ extension ComposeViewController: TextEditorViewTextAttributesDelegate { let highlightMatches = string.matches(pattern: "(?:@([a-zA-Z0-9_]+)|#([^\\s]+))") // not accept :$ to force user input space to make emoji take effect let emojiMatches = string.matches(pattern: "(?:(^:|\\s:)([a-zA-Z0-9_]+):\\s)") + // only accept http/https scheme + let urlMatches = string.matches(pattern: "(?i)https?://\\S+(?:/|\\b)") DispatchQueue.main.async { [weak self] in guard let self = self else { @@ -311,6 +313,27 @@ extension ComposeViewController: TextEditorViewTextAttributesDelegate { } } + for match in urlMatches { + if let name = string.substring(with: match, at: 0) { + os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: handle emoji: %s", ((#file as NSString).lastPathComponent), #line, #function, name) + + // set highlight + var attributes = [NSAttributedString.Key: Any]() + attributes[.foregroundColor] = Asset.Colors.Label.highlight.color + // See `traitCollectionDidChange(_:)` + // set accessibility + if #available(iOS 13.0, *) { + switch self.traitCollection.accessibilityContrast { + case .high: + attributes[.underlineStyle] = NSUnderlineStyle.single.rawValue + default: + break + } + } + attributedString.addAttributes(attributes, range: match.range) + } + } + completion(attributedString) } }