Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change TextField's visibility icon behaviour in order to match more Android EditText's one #979

Merged
merged 1 commit into from
Nov 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "ic_visibility_off_white.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "ic_visibility_off_white@2x.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "ic_visibility_off_white@3x.png",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions Sources/iOS/Icon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public struct Icon {
public static let starHalf = Icon.icon("ic_star_half_white")
public static let videocam = Icon.icon("ic_videocam_white")
public static let visibility = Icon.icon("ic_visibility_white")
public static let visibilityOff = Icon.icon("ic_visibility_off_white")
public static let work = Icon.icon("ic_work_white")

/// CosmicMind icons.
Expand Down
17 changes: 9 additions & 8 deletions Sources/iOS/TextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,9 @@ open class TextField: UITextField {
return
}

visibilityIconButton = IconButton(image: Icon.visibility, tintColor: placeholderNormalColor.withAlphaComponent(isSecureTextEntry ? 0.38 : 0.54))
visibilityIconButton = isSecureTextEntry ? IconButton(image: Icon.visibility, tintColor: placeholderNormalColor.withAlphaComponent(0.54)) : IconButton(image: Icon.visibilityOff, tintColor: placeholderNormalColor.withAlphaComponent(0.54))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a note, I updated this line to:

IconButton(image: isSecureTextEntry ? Icon.visibility : Icon.visibilityOff, tintColor: placeholderNormalColor.withAlphaComponent(0.54))

less code :)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! You're totally right!! Thanks a lot! :)

visibilityIconButton!.contentEdgeInsetsPreset = .none
visibilityIconButton!.pulseAnimation = .none
visibilityIconButton!.pulseAnimation = .centerRadialBeyondBounds
isSecureTextEntry = true
clearButtonMode = .never
rightViewMode = .whileEditing
Expand Down Expand Up @@ -643,12 +643,13 @@ fileprivate extension TextField {
func handleVisibilityIconButton() {
isSecureTextEntry = !isSecureTextEntry

if !isSecureTextEntry {
super.font = nil
font = placeholderLabel.font
}

visibilityIconButton?.tintColor = visibilityIconButton?.tintColor.withAlphaComponent(isSecureTextEntry ? 0.38 : 0.54)
UIView.transition(with: (visibilityIconButton?.imageView)!,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more note, if the developer sets the tint color of the button, it will be changed here to the placeholder color. So this should handle that:

animations: { [weak self] in
                            guard let `self` = self else {
                                return
                            }
                            
                            guard let v = self.visibilityIconButton else {
                                return
                            }
                            
                            v.image = self.isSecureTextEntry ? Icon.visibilityOff?.tint(with: v.tintColor.withAlphaComponent(0.54)) : Icon.visibility?.tint(with: v.tintColor.withAlphaComponent(0.54))
                            
                          },

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right again! I'm sorry, I'm not much used to coding for public libraries ^^'
Anyway, I think that the two icons are inverted in this snippet.

Copy link
Member

@daniel-jonathan daniel-jonathan Nov 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You did great! It is my responsibility to massage it into the framework. Keep it coming ;)

duration: 0.3,
options: .transitionCrossDissolve,
animations: {
self.visibilityIconButton?.image = self.isSecureTextEntry ? Icon.visibilityOff?.tint(with: self.placeholderNormalColor.withAlphaComponent(0.54)) : Icon.visibility?.tint(with: self.placeholderNormalColor.withAlphaComponent(0.54))
},
completion: nil)
}
}

Expand Down