2015-06-05 8 views
13

जब मैं UITextField में स्विफ्ट के साथ टाइपिंग करता हूं तो मैं चरित्र को गिनना चाहता हूं। फील्ड केटाइपिंग (स्विफ्ट) के दौरान लाइव UITextField गिनती कैसे करें?

छवि और लेबल:

enter image description here

मैं पहले से ही UITextField और UILabel रखा है, सिर्फ ढेर अतिप्रवाह पर कोई जानकारी नहीं मिली है, यह भी आप UITextView मैं भी में एक कर सकते हैं यदि इसकी प्रशंसा करना।

उत्तर

17

फ़ंक्शन का उपयोग करने के नीचे आप पाठ क्षेत्र आप गणना करना चाहते हैं की UITextFieldDelegate प्रोटोकॉल को लागू करने की जरूरत है, इस समारोह हर बुलाया जाता UITextField परिवर्तन मूल्य:

आपका वर्ग घोषणा इस

कुछ ऐसा दिखाई देगा
class ViewController: UIViewController, UITextFieldDelegate 

आप इस

@IBOutlet var txtValue: UITextField

के लिए एक @IBOutlet जैसा होना चाहिए 210

viewDidLoad में आप स्वयं

override func viewDidLoad() { 
    super.viewDidLoad() 
    txtValue.delegate = self     
} 

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { 
    let newLength = count(textField.text.utf16) + count(string.utf16) - range.length 
    //change the value of the label 
    mylabel.text = String(newLength) 
    //you can save this value to a global var 
    //myCounter = newLength 
    //return true to allow the change, if you want to limit the number of characters in the text field use something like 
    return newLength <= 25 // To just allow up to 25 characters 
    return true 
} 

के लिए प्रतिनिधि विधि सेट याद रखें इस समारोह सभी UITextField के लिए कहा जाता है, इसलिए यदि आप कई UITextField है आप एक तर्क जोड़ने के लिए डायन भी इस समारोह बुला रहा है जानने की आवश्यकता होगी, बस जोड़ने प्रत्येक UITextField लिए कोई अलग टैग और एक स्विच बयान समारोह

+0

इसे लेबल से कैसे कनेक्ट करें? क्षमा करें मैं शुरुआती हूं – Xrait

+0

ठीक है, मुझे यह मिला, लेकिन मेरे टेक्स्ट फ़ील्ड के नीचे छोटे लेबल को कैसे बदला जाए? – Xrait

+0

क्षमा करें, मुझे नहीं पता कि आप किस लेबल का जिक्र कर रहे हैं। – Icaro

5

इस का उपयोग UITextField के साथ खोजने के लिए फोन केवल अपने पाठ फ़ील्ड इनपुट की अनुमति देगा 14 चार

class ViewController: UIViewController,UITextFieldDelegate { 

@IBOutlet weak var textfield: UITextField! 
@IBOutlet weak var label: UILabel! 
override func viewDidLoad() { 
    super.viewDidLoad() 
    self.label.text = "14" 
    self.textfield.delegate = self 
} 
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { 
    let newLength = count(textField.text.utf16) + count(string.utf16) - range.length 
    if(newLength <= 14){ 
     self.label.text = "\(14 - newLength)" 
     return true 
    }else{ 
     return false 
    } 
} 
} 

और स्क्रीनशॉट

enter image description here

2
In Swift 
viewDidLoad { 
self.yourTextView.delegate = self 
self.updateCharacterCount() 
} 

func updateCharacterCount() { 
    self.yourLabel.text = "\((65) - self.yourTextView.text.characters.count)" 
} 

func textViewDidChange(textView: UITextView) { 
    self.updateCharacterCount() 
} 


func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool { 
    self.updateCharacterCount() 
    return textView.text.characters.count + (text.characters.count - range.length) <= 65 
} 
+0

यह मेरे लिए काम नहीं है –

6

एक बहुत ही सुंदर और स्वच्छ समाधान UITextFieldDelegate का उपयोग कर मौजूद है। मेरा समाधान चयनकर्ताओं की अवधारणा का उपयोग करता है। एक चयनकर्ता में आप अपने कंपाइलर को बताते हैं कि जब कोई विशेष घटना होती है तो प्रदर्शन करने के लिए क्या कार्य/क्रिया होती है। इस मामले में - टेक्स्टफील्ड में टाइपिंग। सुनिश्चित करें कि आपने स्टोरीबोर्ड में टेक्स्टफ़ील्ड प्रतिनिधि सेट किया है।

override func viewDidLoad() { 
    super.viewDidLoad() 
    yourTextField.addTarget(self, action: #selector(YourViewController.textFieldDidChange(_:)), forControlEvents: UIControlEvents.EditingChanged) 
} 

func textFieldDidChange(textField : UITextField){ 
    label.text = yourTextField.text?.characters.count 
} 
0

यहां स्विफ्ट 3/4 में आप इसे कैसे कर सकते हैं।

सबसे पहले, सुनिश्चित करें कि आपको अपना टेक्स्टफ़िल्ल्ड का प्रतिनिधि viewDidLoad में सेट किया गया है।

yourTextField.delegate = self 

फिर, को लागू shouldChangeCharactersIn:

extension YourViewController: UITextFieldDelegate { 
    func textField(_ textFieldToChange: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { 
     // limit to 4 characters 
     let characterCountLimit = 4 

     // We need to figure out how many characters would be in the string after the change happens 
     let startingLength = textFieldToChange.text?.characters.count ?? 0 
     let lengthToAdd = string.characters.count 
     let lengthToReplace = range.length 

     let newLength = startingLength + lengthToAdd - lengthToReplace 

     return newLength <= characterCountLimit 
    } 
} 

अतिरिक्त विवरण here

0

स्विफ्ट 4 UITextView परिवर्तन पाठ घटना के लिए UITextViewDelegate

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool { 

} 
संबंधित मुद्दे