-
Notifications
You must be signed in to change notification settings - Fork 197
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
Fixes UISegmentedControl extension crash and improves efficiency #398
Changes from all commits
b297775
a3f88b4
620f623
ee18bc4
c0fe6bb
e8ef86a
6753df1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -146,20 +146,20 @@ extension UISegmentedControl { | |
|
||
// `UISegmentedControl` has terrible generics ([NSObject: AnyObject]? or [AnyHashable: Any]?) on | ||
/// `titleTextAttributes`, so use a helper in Swift 3+ | ||
@nonobjc final func bon_titleTextAttributes(for state: UIControl.State) -> StyleAttributes { | ||
let attributes = titleTextAttributes(for: state) ?? [:] | ||
var result: StyleAttributes = [:] | ||
for value in attributes { | ||
#if swift(>=4.2) | ||
result[value.key] = value | ||
#else | ||
guard let string = value.key as? StyleAttributes.Key else { | ||
fatalError("Can not convert key \(value.key) to String") | ||
@nonobjc final func bon_titleTextAttributes(for state: UIControl.State) -> StyleAttributes? { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This changes the public API, would it be possible keep this the same? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, I have reverted the changes to the signature of that method. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just realized that method is actually There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also it looks like the tests are failing because Zev's twitter account is unreachable.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be related to #373 @ZevEisenberg |
||
guard let attributes = titleTextAttributes(for: state) else { return nil } | ||
#if swift(>=4.2) | ||
return attributes | ||
#else | ||
var result: StyleAttributes = [:] | ||
for attribute in attributes { | ||
guard let string = attribute.key as? StyleAttributes.Key else { | ||
fatalError("Can not convert key \(attribute.key) to String") | ||
} | ||
result[string] = value | ||
#endif | ||
} | ||
return result | ||
result[string] = attribute.value | ||
} | ||
return result | ||
#endif | ||
} | ||
|
||
/// Adapt `attributedTitle`, for all control states, to the specified trait collection. | ||
|
@@ -168,7 +168,7 @@ extension UISegmentedControl { | |
@objc(bon_updateTextForTraitCollection:) | ||
public func adaptText(forTraitCollection traitCollection: UITraitCollection) { | ||
for state in UIControl.State.commonStates { | ||
let attributes = bon_titleTextAttributes(for: state) | ||
guard let attributes = bon_titleTextAttributes(for: state) else { continue } | ||
let newAttributes = NSAttributedString.adapt(attributes: attributes, to: traitCollection) | ||
setTitleTextAttributes(newAttributes, for: state) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this comment need to be updated at all in light of these changes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fixed an error inside that method. When I changed the return type back to being non-optional I forgot to default to an empty dictionary when nil. I also added a test for segmented control similar to the other UIKit tests.
As for the comment, I can't speak to whether it is still appropriate or not, although I can say in Swift 5
titleTextAttributes(for:)
returns[NSAttributedFont.Key: Any]?
.