如何在Swift UI中粘贴来自不同应用程序的文本字段



我为6位数字创建了一个OTP字段,我想启用粘贴功能,例如我想从imessage复制文本到我的应用程序的文本字段。我认为这是自动在文本字段,但当我双击在文本字段对话框要求粘贴文本不出现。我的剪贴板不是空的,因为我在其他文本字段中尝试了它,而不是SwiftUI,它工作了。

由于Xcode的限制,我的textfield被创建为许多变通视图。我只能使用Xcode 12.3。

p>

果有人也可以帮助我为什么完成我的十进制键盘缺少按钮。 下面是我的代码:
import SwiftUI
@available(iOS 13.0, *)
class OTPViewModel: ObservableObject {

var numberOfFields: Int

init(numberOfFields: Int = 6) {
self.numberOfFields = numberOfFields
}

@Published var otpField = "" {
didSet {
guard otpField.count <= numberOfFields,
otpField.last?.isNumber ?? true else {
otpField = oldValue
return
}
if otpField.count == numberOfFields {
hideKeyboard()
}
}
}

@Published var isEditing = false

func otp(digit: Int) -> String {
guard otpField.count >= digit else {
return ""
}
return String(Array(otpField)[digit - 1])
}

func hideKeyboard() {
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
}
}
@available(iOS 13.0, *)
struct OTPView: View {
@ObservedObject var viewModel = OTPViewModel()

private let textBoxWidth: CGFloat = 41
private let textBoxHeight = UIScreen.main.bounds.width / 8
private let spaceBetweenLines: CGFloat = 16
private let paddingOfBox: CGFloat = 1
private var textFieldOriginalWidth: CGFloat {
(textBoxWidth + CGFloat(18)) * CGFloat(viewModel.numberOfFields)
}

var body: some View {
Background {
VStack {
ZStack {
HStack (spacing: spaceBetweenLines) {
ForEach(1 ... viewModel.numberOfFields, id: .self) { digit in
otpText(
text: viewModel.otp(digit: digit),
isEditing: viewModel.isEditing,
beforeCursor: digit - 1 < viewModel.otpField.count,
afterCursor: viewModel.otpField.count < digit - 1
)
}
} //: HSTACK
TextField("", text: $viewModel.otpField) { isEditing in
viewModel.isEditing = isEditing
}
.font(Font.system(size: 50, design: .default))
.offset(y: 10)
.frame(width: viewModel.isEditing ? 0 : textFieldOriginalWidth, height: textBoxHeight)
.textContentType(.oneTimeCode)
.foregroundColor(.clear)
.background(Color.clear)
.keyboardType(.decimalPad)
} //: ZSTACK
} //: VSTACK
} //: Background
.onTapGesture {
viewModel.hideKeyboard()
}
}

struct Background<Content: View>: View {
private var content: Content
init(@ViewBuilder content: @escaping () -> Content) {
self.content = content()
}
var body: some View {
Color.clear
.contentShape(Rectangle())
.frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
.overlay(content)
}
}

@available(iOS 13.0, *)
private func otpText(
text: String,
isEditing: Bool,
beforeCursor: Bool,
afterCursor: Bool
) -> some View {
return Text(text)
.font(Font.custom("GTWalsheim-Regular", size: 34))
.frame(width: textBoxWidth, height: textBoxHeight)
.background(VStack{
Spacer()
.frame(height: 65)
ZStack {
Capsule()
.frame(width: textBoxWidth, height: 2)
.foregroundColor(Color(hex: "#BCBEC0"))

Capsule()
.frame(width: textBoxWidth, height: 2)
.foregroundColor(Color(hex: "#367878"))
.offset(x: (beforeCursor ? textBoxWidth : 0) + (afterCursor ? -textBoxWidth : 0))
.animation(.easeInOut, value: [beforeCursor, afterCursor])
.opacity(isEditing ? 1 : 0)
} //: ZSTACK
.clipped()
})
.padding(paddingOfBox)
}
}

粘贴功能是存在的,但由于我的textfield的框架。这个球太出界了,在屏幕上看不到。我调整了框架,粘贴现在出现了。我还修改了强调色来隐藏光标。

var body: some View {
VStack {
ZStack {
HStack (spacing: spaceBetweenLines) {
ForEach(1 ... viewModel.numberOfFields, id: .self) { digit in
otpText(
text: viewModel.otp(digit: digit),
isEditing: viewModel.isEditing,
beforeCursor: digit - 1 < viewModel.otpField.count,
afterCursor: viewModel.otpField.count < digit - 1
)
}
} //: HSTACK
TextField("", text: $viewModel.otpField) { isEditing in
viewModel.isEditing = isEditing
}
.font(Font.system(size: 50, design: .default))
.offset(y: 10)
.frame(width: textFieldOriginalWidth, height: textBoxHeight) // What I changed
.textContentType(.oneTimeCode)
.foregroundColor(.clear)
.background(Color.clear)
.keyboardType(.decimalPad)
.accentColor(.clear)
} //: ZSTACK
} //: VSTACK

相关内容

  • 没有找到相关文章

最新更新