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.

163 lines
5.1 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: -
struct PhoneInputView: View {
@Binding var phoneNumber: String
@Binding var message: String
let inputType: PhoneInputType
@FocusState var focusedField: PhoneField?
//
enum PhoneInputType: String, CaseIterable {
case phone = "Phone"
case sms = "SMS"
var displayName: String {
switch self {
case .phone: return "电话"
case .sms: return "短信"
}
}
var icon: String {
switch self {
case .phone: return "phone"
case .sms: return "message"
}
}
var placeholder: String {
switch self {
case .phone: return "+86 138 0013 8000"
case .sms: return "输入短信内容"
}
}
var hint: String {
switch self {
case .phone: return "输入电话号码,支持国际格式"
case .sms: return "输入短信内容,将生成可发送的链接"
}
}
}
//
enum PhoneField: Hashable {
case phoneNumber, message
}
var body: some View {
VStack(spacing: 16) {
//
HStack {
Image(systemName: inputType.icon)
.font(.title2)
.foregroundColor(.blue)
VStack(alignment: .leading, spacing: 2) {
Text(inputType.displayName)
.font(.headline)
.foregroundColor(.primary)
Text(inputType.hint)
.font(.caption)
.foregroundColor(.secondary)
}
Spacer()
}
.padding(.horizontal, 12)
.padding(.vertical, 8)
.background(
RoundedRectangle(cornerRadius: 8)
.fill(Color.blue.opacity(0.1))
)
// ()
VStack(alignment: .leading, spacing: 8) {
HStack {
Text("电话号码")
.font(.subheadline)
.foregroundColor(.primary)
Text("*")
.foregroundColor(.red)
Spacer()
}
TextField(inputType.placeholder, text: $phoneNumber)
.textFieldStyle(RoundedBorderTextFieldStyle())
.keyboardType(.phonePad)
.focused($focusedField, equals: .phoneNumber)
}
// (SMS)
if inputType == .sms {
VStack(alignment: .leading, spacing: 8) {
HStack {
Text("短信内容")
.font(.subheadline)
.foregroundColor(.primary)
Spacer()
}
TextField("输入短信内容", text: $message)
.textFieldStyle(RoundedBorderTextFieldStyle())
.focused($focusedField, equals: .message)
}
}
//
VStack(alignment: .leading, spacing: 8) {
HStack {
Image(systemName: "info.circle")
.font(.caption)
.foregroundColor(.blue)
Text("格式说明")
.font(.caption)
.foregroundColor(.primary)
Spacer()
}
Text(getFormatHint())
.font(.caption)
.foregroundColor(.secondary)
.lineLimit(nil)
}
.padding(.horizontal, 12)
.padding(.vertical, 8)
.background(
RoundedRectangle(cornerRadius: 8)
.fill(Color.blue.opacity(0.1))
)
}
.toolbar {
ToolbarItemGroup(placement: .keyboard) {
Spacer()
Button("完成") {
focusedField = nil
}
.foregroundColor(.blue)
.font(.system(size: 16, weight: .medium))
}
}
}
private func getFormatHint() -> String {
switch inputType {
case .phone:
return "• 支持国际格式:+86 138 0013 8000\n• 或本地格式138 0013 8000\n• 将生成 tel: 链接"
case .sms:
return "• 输入电话号码和短信内容\n• 将生成 sms: 链接\n• 用户点击可直接发送短信"
}
}
}
#Preview {
PhoneInputView(
phoneNumber: .constant(""),
message: .constant(""),
inputType: .phone
)
}