feat: add URL highlight for text editor

This commit is contained in:
CMK 2021-03-15 13:44:42 +08:00
parent a6e4b0bfb1
commit 8eb24871c5
1 changed files with 23 additions and 0 deletions

View File

@ -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)
}
}