You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
3.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import SwiftUI
// MARK: - URL
struct URLInputView: View {
@Binding var url: String
@FocusState var isUrlFieldFocused: Bool
var body: some View {
VStack(spacing: 16) {
// URL
VStack(alignment: .leading, spacing: 8) {
HStack {
Text("网址")
.font(.subheadline)
.foregroundColor(.primary)
Text("*")
.foregroundColor(.red)
Spacer()
}
TextField("https://www.example.com", text: $url)
.textFieldStyle(RoundedBorderTextFieldStyle())
.keyboardType(.URL)
.autocapitalization(.none)
.focused($isUrlFieldFocused)
.onChange(of: url) { newValue in
// https://
if !newValue.isEmpty && !newValue.hasPrefix("http://") && !newValue.hasPrefix("https://") {
url = "https://" + newValue
}
}
}
//
VStack(alignment: .leading, spacing: 8) {
HStack {
Image(systemName: "info.circle")
.font(.caption)
.foregroundColor(.blue)
Text("格式说明")
.font(.caption)
.foregroundColor(.primary)
Spacer()
}
Text("• 可以输入完整URLhttps://www.example.com\n• 或输入域名www.example.com\n• 系统会自动添加https://前缀")
.font(.caption)
.foregroundColor(.secondary)
.lineLimit(nil)
}
.padding(.horizontal, 12)
.padding(.vertical, 8)
.background(
RoundedRectangle(cornerRadius: 8)
.fill(Color.blue.opacity(0.1))
)
// URL
if !url.isEmpty {
VStack(alignment: .leading, spacing: 8) {
HStack {
Image(systemName: "link")
.font(.caption)
.foregroundColor(.green)
Text("预览URL")
.font(.caption)
.foregroundColor(.primary)
Spacer()
}
Text(url)
.font(.caption)
.foregroundColor(.green)
.padding(.horizontal, 12)
.padding(.vertical, 8)
.background(
RoundedRectangle(cornerRadius: 6)
.fill(Color.green.opacity(0.1))
)
}
}
}
.toolbar {
ToolbarItemGroup(placement: .keyboard) {
Spacer()
Button("完成") {
isUrlFieldFocused = false
}
.foregroundColor(.blue)
.font(.system(size: 16, weight: .medium))
}
}
}
}
#Preview {
URLInputView(url: .constant(""))
}