Код: Выделить всё
import SwiftUI
struct BaseInputOTP: View {
var isSecure: Bool = false
var isError: Bool = false
@Binding var value: String
@FocusState var focus: Bool
var body: some View {
HStack(spacing: 8) {
ForEach(0.. some View {
ZStack {
if value.count > index && !isSecure {
let startIndex = value.startIndex
let charIndex = value.index(startIndex, offsetBy: index)
let charToString = String(value[charIndex])
Text(charToString)
.font(FontFigtreeManager.heading5A)
.foregroundColor(Constants.ColorNeutralColorDark100)
} else {
Text(" ")
}
}
.padding(.horizontal, 18)
.padding(.vertical, 17)
.frame(maxWidth: .infinity, minHeight: 64, maxHeight: 64, alignment: .center)
.cornerRadius(Constants.RadiusMd)
.overlay {
let status = (focus && value.count == index)
let strokeColor = isError
? Color(red: 0.82, green: 0.26, blue: 0.26)
: Color(red: 0.1, green: 0.1, blue: 0.1).opacity(status ? 1 : 0.2)
RoundedRectangle(cornerRadius: Constants.RadiusMd)
.inset(by: 0.5)
.stroke(strokeColor, lineWidth: 1)
.animation(.easeInOut(duration: 0.2), value: isError)
.background (
Group{
if status {
BlinkingCursor()
}
}
)
if value.count > index && isSecure {
Circle()
.fill(Color(red: 0.1, green: 0.1, blue: 0.1))
.frame(width: 24, height: 24)
}
}
}
}
struct BlinkingCursor: View {
@State private var opacity: Double = 1.0
var body: some View {
Text("|")
.font(FontFigtreeManager.heading5A)
.foregroundColor(Color(red: 0.1, green: 0.1, blue: 0.1))
.opacity(opacity)
.onAppear {
withAnimation(Animation.linear(duration: 0.6).repeatForever(autoreverses: true)) {
opacity = 0.2
}
}
}
}
extension Binding where Value == String {
func limit(_ length: Int) -> Self {
if self.wrappedValue.count > length {
DispatchQueue.main.async {
self.wrappedValue = String(self.wrappedValue.prefix(length))
}
}
return self
}
}
Подробнее здесь: https://stackoverflow.com/questions/793 ... -in-ios-14