详情页释义增加搜索按钮,应用内打开 Google 搜索

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-07-19 21:48:20 +08:00
parent 43a26da659
commit 152dcd9aac
@@ -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