SwiftUI कैसे चेतन करने के लिए प्रत्येक चरित्र में पाठ फ़ील्ड?

0

सवाल

के रूप में एक उपयोगकर्ता प्रकार के अक्षरों में एक पाठ फ़ील्ड, मैं होगा की तरह कुछ प्रदर्शित करने के लिए एनीमेशन पर प्रत्येक नव टाइप चरित्र (जैसे थोड़े कैसे नकदी एप्लिकेशन संख्या उत्साहित लेकिन मैं इसे लागू करने के लिए वर्णमाला के अक्षर के रूप में अच्छी तरह से).

enter image description here

क्या यह संभव है ऐसा करने के लिए, में SwiftUI? मेरे अंतर्ज्ञान है कि मैं हो सकता है के लिए पुल करने के लिए UIKit के लिए और अधिक सूक्ष्म का उपयोग करने के लिए एक textfield'एस तत्व है, लेकिन नहीं यकीन है कि कैसे करने के लिए वास्तव में है कि लागू.

swift swiftui
2021-11-23 14:54:17
1

सबसे अच्छा जवाब

1

आप कर सकते हैं बस बनाने के लिए एक "नकली" TextField है कि प्रतीत होता है पर असली एक है. तो शो में पात्रों में से एक ForEach.

यह के साथ किया जाता है FocusState iOS में 15

@available(iOS 15.0, *)
struct AnimatedInputView: View {
    @FocusState private var isFocused: Int?
    @State var text: String = ""
    //If all the fonts match the cursor is better aligned 
    @State var font: Font = .system(size: 48, weight: .bold, design: .default)
    @State var color: Color = .gray
    var body: some View {
        HStack(alignment: .center, spacing: 0){
            //To maintain size in between the 2 views
            Text(text)
                .font(font)
                .opacity(0)
                .overlay(
                    //This textField will be invisible
                    TextField("", text: $text)
                        .font(font)
                        .foregroundColor(.clear)
                        .focused($isFocused, equals: 1)
                )
                .background(
                    ZStack{
                        HStack(alignment: .center, spacing: 0, content: {
                            //You need an array of unique/identifiable characters
                            let uniqueArray = text.uniqueCharacters()
                            ForEach(uniqueArray, id: \.id, content: { char in
                                CharView(char: char.char, isLast: char == uniqueArray.last, font: font)
                                
                            })
                        })
                    }.opacity(1)
                        .minimumScaleFactor(0.1)
                    
                )
            
                .onAppear(perform: {
                    //Bring focus to the hidden TextField
                    DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
                        isFocused = 1
                    })
                })
        }
        .padding()
        .border(color)
        .font(.title)
        //Bring focus to the hidden textfield
        .onTapGesture {
            isFocused = 1
        }
    }
}
struct CharView: View{
    var char: Character
    var isLast: Bool
    var font: Font
    @State var scale: CGFloat = 0.75
    var body: some View{
        Text(char.description)
            .font(font)
            .minimumScaleFactor(0.1)
            .scaleEffect(scale)
            .onAppear(perform: {
                //Animate only if last character
                if isLast{
                    withAnimation(.linear(duration: 0.5)){
                        scale = 1
                    }
                }else{
                    scale = 1
                }
            })
    }
}
@available(iOS 15.0, *)
struct AnimatedInputView_Previews: PreviewProvider {
    static var previews: some View {
        AnimatedInputView()
    }
}
//Convert String to Unique characers
extension String{
    func uniqueCharacters() -> [UniqueCharacter]{
        let array: [Character] = Array(self)
        return array.uniqueCharacters()
    }
    func numberOnly() -> String {
        self.trimmingCharacters(in: CharacterSet(charactersIn: "-0123456789.").inverted)
    }
    
}
extension Array where Element == Character {
    
    func uniqueCharacters() -> [UniqueCharacter]{
        var array: [UniqueCharacter] = []
        
        for char in self{
            array.append(UniqueCharacter(char: char))
        }
        return array
    }
    
}

//String/Characters can be repeating so yu have to make them a unique value
struct UniqueCharacter: Identifiable, Equatable{
    var char: Character
    var id: UUID = UUID()
}

यहाँ एक नमूना संस्करण है कि. केवल संख्या की तरह कैलकुलेटर नमूना

import SwiftUI

@available(iOS 15.0, *)
struct AnimatedInputView: View {
    @FocusState private var isFocused: Int?
    @State var text: String = ""
    //If all the fonts match the cursor is better aligned 
    @State var font: Font = .system(size: 48, weight: .bold, design: .default)
    @State var color: Color = .gray
    var body: some View {
        HStack(alignment: .center, spacing: 0){
            Text("$").font(font)
            //To maintain size in between the 2 views
            Text(text)
                .font(font)
                .opacity(0)
                .overlay(
                    //This textField will be invisible
                    TextField("", text: $text)
                        .font(font)
                        .foregroundColor(.clear)
                        .focused($isFocused, equals: 1)
                        .onChange(of: text, perform: { value in
                               if Double(text) == nil{
                                   //Leaves the negative and decimal period
                                   text = text.numberOnly()
                               }
                               //This condition can be improved.
                               //Checks for 2 occurences of the decimal period
                               //Possible solution
                               while text.components(separatedBy: ".").count > 2{
                                   color = .red
                                   text.removeLast()
                               }

                               //This condition can be improved.
                               //Checks for 2 occurences of the negative
                               //Possible solution
                               while text.components(separatedBy: "-").count > 2{
                                   color = .red
                                   text.removeLast()
                               }
                               color = .gray

                           })
                )
                .background(
                    ZStack{
                        HStack(alignment: .center, spacing: 0, content: {
                            //You need an array of unique/identifiable characters
                            let uniqueArray = text.uniqueCharacters()
                            ForEach(uniqueArray, id: \.id, content: { char in
                                CharView(char: char.char, isLast: char == uniqueArray.last, font: font)
                                
                            })
                        })
                    }.opacity(1)
                        .minimumScaleFactor(0.1)
                    
                )
            
                .onAppear(perform: {
                    //Bring focus to the hidden TextField
                    DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
                        isFocused = 1
                    })
                })
        }
        .padding()
        .border(color)
        .font(.title)
        //Bring focus to the hidden textfield
        .onTapGesture {
            isFocused = 1
        }
    }
}
2021-11-24 02:45:46

धन्यवाद । क्या आपको लगता है कि करने के लिए सक्षम संपादन, हम कर सकते हैं किसी भी तरह प्रोग्राम के रूप में बदलने के लिए Z-सूचकांक के पाठ फ़ील्ड और पाठ ओवरले? शायद एक ZStack के बजाय ओवरले. और जब उपयोगकर्ता पर क्लिक करता है, पाठ, हम बस कर सकते हैं लाने के लिए पाठ फ़ील्ड के लिए सामने के लिए संपादन, और अद्यतन के चार-सरणी प्रति संपादित करें... यह जटिल है, लेकिन धन्यवाद आप के लिए समाधान!
PipEvangelist

संभव है, लेकिन बहुत जटिल होने की संभावना से ग्रस्त करने के लिए और अधिक कीड़े ।
lorem ipsum

@PipEvangelist वास्तव में, मैं समझ से बाहर एक और तरीका यह करना है. यह लग रहा है एक छोटे से थोड़ा दूर है लेकिन एक बेहतर संस्करण है । यह संपादन की अनुमति देता है. कर्सर सिर्फ एक बिट बंद
lorem ipsum

धन्यवाद! यह शानदार है
PipEvangelist

अन्य भाषाओं में

यह पृष्ठ अन्य भाषाओं में है

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................