diff --git a/LearnEnglish/LearnEnglish/Views/WordDetailViewController.swift b/LearnEnglish/LearnEnglish/Views/WordDetailViewController.swift index 149186a..ceefae9 100644 --- a/LearnEnglish/LearnEnglish/Views/WordDetailViewController.swift +++ b/LearnEnglish/LearnEnglish/Views/WordDetailViewController.swift @@ -1,3 +1,4 @@ +import SafariServices import UIKit final class WordDetailViewController: UIViewController { @@ -125,6 +126,20 @@ final class WordDetailViewController: UIViewController { meaningLabel.font = .preferredFont(forTextStyle: .title2) meaningLabel.numberOfLines = 0 + var searchConfig = UIButton.Configuration.plain() + searchConfig.baseForegroundColor = Theme.Color.accent + searchConfig.image = UIImage(systemName: "magnifyingglass", + withConfiguration: UIImage.SymbolConfiguration(pointSize: 15, weight: .medium)) + let searchButton = UIButton(type: .system) + searchButton.configuration = searchConfig + searchButton.addAction(UIAction { [weak self] _ in self?.searchMeaning() }, for: .touchUpInside) + searchButton.setContentHuggingPriority(.required, for: .horizontal) + + let meaningRow = UIStackView(arrangedSubviews: [meaningLabel, searchButton]) + meaningRow.axis = .horizontal + meaningRow.alignment = .top + meaningRow.spacing = 8 + let divider = HairlineView() let exampleTitle = makeCaption("例句") @@ -154,7 +169,7 @@ final class WordDetailViewController: UIViewController { exampleRow.alignment = .top exampleRow.spacing = 8 - let stack = UIStackView(arrangedSubviews: [meaningTitle, meaningLabel, divider, exampleTitle, exampleRow]) + let stack = UIStackView(arrangedSubviews: [meaningTitle, meaningRow, divider, exampleTitle, exampleRow]) stack.axis = .vertical stack.spacing = 16 stack.setCustomSpacing(6, after: meaningTitle) @@ -171,6 +186,13 @@ final class WordDetailViewController: UIViewController { return card } + private func searchMeaning() { + Haptics.selection() + guard let query = word.meaning.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed), + let url = URL(string: "https://www.google.com/search?q=\(query)") else { return } + present(SFSafariViewController(url: url), animated: true) + } + private func makeCaption(_ text: String) -> UILabel { let label = UILabel() label.text = text