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.
58 lines
1.6 KiB
58 lines
1.6 KiB
import SwiftUI
|
|
|
|
// MARK: - 输入标题组件
|
|
struct InputTitleView: View {
|
|
let title: String
|
|
let isRequired: Bool
|
|
let icon: String?
|
|
|
|
init(_ title: String, isRequired: Bool = false, icon: String? = nil) {
|
|
self.title = title
|
|
self.isRequired = isRequired
|
|
self.icon = icon
|
|
}
|
|
|
|
var body: some View {
|
|
HStack(spacing: 8) {
|
|
if let icon = icon {
|
|
Image(systemName: icon)
|
|
.font(.subheadline)
|
|
.foregroundColor(.blue)
|
|
}
|
|
|
|
Text(title)
|
|
.font(.subheadline)
|
|
.foregroundColor(.primary)
|
|
|
|
if isRequired {
|
|
Text("*")
|
|
.foregroundColor(.red)
|
|
.font(.subheadline)
|
|
}
|
|
|
|
Spacer()
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - 预定义的标题样式
|
|
extension InputTitleView {
|
|
static func required(_ title: String, icon: String? = nil) -> InputTitleView {
|
|
InputTitleView(title, isRequired: true, icon: icon)
|
|
}
|
|
|
|
static func optional(_ title: String, icon: String? = nil) -> InputTitleView {
|
|
InputTitleView(title, isRequired: false, icon: icon)
|
|
}
|
|
}
|
|
|
|
#Preview {
|
|
VStack(spacing: 16) {
|
|
InputTitleView.required("必填字段", icon: "star.fill")
|
|
InputTitleView.optional("可选字段", icon: "circle")
|
|
InputTitleView("普通标题")
|
|
InputTitleView.required("邮箱地址", icon: "envelope")
|
|
InputTitleView.required("密码", icon: "lock")
|
|
}
|
|
.padding()
|
|
} |