I have three global variables:
var activeTextField = UITextField() // stores the last textFieldDidBeginEditing
var activeTextView = UITextView() // stores the last textViewDidBeginEditing
var lastTextKindIsTextView = true // indicates if the last text used is a UITextView
I would like to count the number of selected characters of the first responder text field or text view. I tryed with this code but it don't work because activeField is out of scope. Is there a way to do this?
if lastTextKindIsTextView {
let activeField = activeTextView
} else {
let activeField = activeTextField
}
// see the length of the selected string
var strLength = 0
// don't work because activeField is out of scope:
if let textRange = activeField.selectedTextRange {
let selectedText = activeField.text(in: textRange)
strLength = (selectedText?.utf16.count)!
}
Aucun commentaire:
Enregistrer un commentaire