Reputation: 272372
override func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
label.text = textView.text
return true;
}
With this code, I seem to be a character behind.
Upvotes: 0
Views: 106
Reputation: 5148
You can use NSString
replacingOccurrences(of:with:)
to get typing text, like this
Swift2
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
let originText = textView.text as NSString
let replacingText = originText.stringByReplacingCharactersInRange(range, withString: text)
label.text = replacingText
return true
}
Swift3
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
let originText = textView.text as NSString
let replacingText = originText.replacingCharacters(in: range, with: text)
label.text = replacingText
return true
}
Upvotes: -1
Reputation: 1195
shouldChangeTextInRange
will be called before the text field's text value is changed.
Try putting your code in textViewDidChange instead:
func textViewDidChange(textView: UITextView) {
label.text = textView.text
}
Upvotes: 3
Reputation: 52237
You are setting the previous textView's text. try:
override func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
label.text = text
return true;
}
as this method is called before the textView's new text is propagated.
Upvotes: 1