jobandtalent / AnimatedTextInput

Animated UITextField and UITextView replacement for iOS
MIT License
760 stars 128 forks source link

Performance issue during text input #90

Closed Kirow closed 6 years ago

Kirow commented 6 years ago

During fast and continuous text input my fps drop down to ~zero (iPhone5S). I've tracked down bottleneck in AnimatedTextField.swift:

@objc fileprivate func textFieldDidChange() {
        if let text = text {
            var cursorPosition: Int?
            if let selectedRange = self.selectedTextRange {
                cursorPosition = self.offset(from: self.beginningOfDocument, to: selectedRange.start)
            }
            attributedText = NSAttributedString(string: text, attributes: textAttributes)
            if let cursorPosition = cursorPosition, let newPosition = self.position(from: self.beginningOfDocument, offset: cursorPosition) {
                self.selectedTextRange = self.textRange(from: newPosition, to: newPosition)
            }
        }
        textInputDelegate?.textInputDidChange(textInput: self)
    }

I reduced code to:

@objc fileprivate func textFieldDidChange() {
     textInputDelegate?.textInputDidChange(textInput: self)
}

and there is no any visual changes for text, but freezes are gone.

Could you please verify if this part of code doing anything necessary?

var cursorPosition: Int?
if let selectedRange = self.selectedTextRange {
   cursorPosition = self.offset(from: self.beginningOfDocument, to: selectedRange.start)
}
attributedText = NSAttributedString(string: text, attributes: textAttributes)
if let cursorPosition = cursorPosition, let newPosition = self.position(from: self.beginningOfDocument, offset: cursorPosition) {
   self.selectedTextRange = self.textRange(from: newPosition, to: newPosition)
}
victorBaro commented 6 years ago

Hello @Kirow Thanks a lot. You are totally right. I will update it asap. Sorry it took me so long to answer.