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("• 可以输入完整URL:https://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("")) }