Skyscanner / SkyFloatingLabelTextField

A beautiful and flexible text field control implementation of "Float Label Pattern". Written in Swift.
Apache License 2.0
4.09k stars 542 forks source link

Title not all caps option #194

Closed lawgimenez closed 6 years ago

lawgimenez commented 6 years ago

ℹ Please fill out this template when filing an issue. All lines beginning with an ℹ symbol instruct you with what info we expect. Please remove this line and all above before submitting.

Before you start, are you using the latest SkyFloatingLabelTextField release?

Issues are for feature requests, and bugs; questions should go to Stack Overflow with the tag skyfloatinglabeltextfield.

Report

What did you do?

ℹ I implemented this library and I want an option where the title would not be in all caps

What did you expect to happen?

ℹ Title to not be in all caps option

What happened instead?

ℹ Can't find an option for disabling all caps in title

Environment

ℹ Please fill in this section with information about your environment

Xcode Version: 9.2 SkyFloatingLabelTextField Version: 3.4.0 Deployment Target: 11.2 Base SDK: 11.2 Method of Integration: CocoaPods

Project that demonstrates the issue

ℹ Please link to a project we can download that reproduces the issue if applicable.

k0nserv commented 6 years ago

Hey @lawgimenez ,

just set the titleFormatter to a function that does not modify the title and you're good

freakdragon commented 6 years ago

I didn't like it too. That's why I've changed the "needToUpperCasePlaceholder" function and added "needToUpperCasePlaceholder" variable:

@IBInspectable dynamic open var needToUpperCasePlaceholder: Bool = false {
        didSet {
        }
    }
open var titleFormatter: ((Bool, String) -> String) = { (needToUpperCasePlaceholder: Bool, text: String) -> String in
        return needToUpperCasePlaceholder ? text.uppercased() : text
    }

When I changed it I had to change the "updateTitleLabel", "titleOrPlaceholder" and "selectedTitleOrTitlePlaceholder" functions:

fileprivate func updateTitleLabel(_ animated: Bool = false) {

        var titleText: String? = nil
        if hasErrorMessage {
            titleText = titleFormatter(false, errorMessage!)
        } else {
            if editingOrSelected {
                titleText = selectedTitleOrTitlePlaceholder()
                if titleText == nil {
                    titleText = titleOrPlaceholder()
                }
            } else {
                titleText = titleOrPlaceholder()
            }
        }
        titleLabel.text = titleText

        updateTitleVisibility(animated)
    }

fileprivate func titleOrPlaceholder() -> String? {
        guard let title = title ?? placeholder else {
            return nil
        }
        return titleFormatter(needToUpperCasePlaceholder, title)
    }

fileprivate func selectedTitleOrTitlePlaceholder() -> String? {
        guard let title = selectedTitle ?? title ?? placeholder else {
            return nil
        }
        return titleFormatter(needToUpperCasePlaceholder, title)
    }
prasanth270 commented 6 years ago

Like @k0nserv suggested, we can use titleFormatter: self.password.titleFormatter = { (text: String) in return text }

wagnercasey commented 6 years ago

@lawgimenez set the formatter like this textField.titleFormatter = { $0 }

ohayon commented 6 years ago

Worth noting - this needs to be set before the title itself is set, otherwise the titleFormatter block doesn't get called. 🚀