apasccon / SearchTextField

UITextField subclass with autocompletion suggestions list
MIT License
1.15k stars 253 forks source link

Fix empty placeholder crash, and remove deprecated method from iOS 13 #202

Open benck opened 3 years ago

benck commented 3 years ago

When the placeholder for the textfield is set to an empty string (textField.placeholder = ""). In the buildPlaceholderLabel() method, using attribute(.foregroundColor, at: 0, effectiveRange: nil) for an empty string will cause crash.

            if let placeholderColor = self.attributedPlaceholder?.attribute(NSAttributedString.Key.foregroundColor, at: 0, effectiveRange: nil) as? UIColor {
                placeholderLabel?.textColor = placeholderColor
            }

So I add the code to check if the attributedPlaceholder is empty before calling attribute() method.

            if let attributedPlaceholder = self.attributedPlaceholder, attributedPlaceholder.length > 0,
               let placeholderColor = attributedPlaceholder.attribute(.foregroundColor, at: 0, effectiveRange: nil) as? UIColor {
                placeholderLabel?.textColor = placeholderColor
            }

This branch also merge @pateldhruvil96 's fix for Swift Package Manager support, and remove deprecated method from iOS 13.