Skip to content

Commit

Permalink
Merge remote-tracking branch 'rzulkoski/bugfix/segmented-control' int…
Browse files Browse the repository at this point in the history
…o rzulkoski-bugfix/segmented-control
  • Loading branch information
chrisballinger committed Jun 30, 2021
2 parents 5bae129 + 6753df1 commit 99cca88
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
3 changes: 2 additions & 1 deletion BonMot.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ Pod::Spec.new do |s|
s.license = 'MIT'
s.author = { "Zev Eisenberg" => "zev@zeveisenberg.com" }
s.source = { :git => "https://github.com/Rightpoint/BonMot.git", :tag => s.version.to_s }
s.social_media_url = 'https://twitter.com/ZevEisenberg'
# Setting the twitter url is causing builds to fail due to not being able to reach twitter.
# s.social_media_url = 'https://twitter.com/ZevEisenberg'
s.requires_arc = true

s.ios.deployment_target = '11.0'
Expand Down
11 changes: 3 additions & 8 deletions Sources/UIKit/AdaptableTextContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,8 @@ 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 {
result[value.key] = value
}
return result
@nonobjc final func bon_titleTextAttributes(for state: UIControl.State) -> StyleAttributes? {
titleTextAttributes(for: state)
}

/// Adapt `attributedTitle`, for all control states, to the specified trait collection.
Expand All @@ -132,7 +127,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)
}
Expand Down
25 changes: 24 additions & 1 deletion Tests/UIKitBonMotTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,30 @@ class UIKitBonMotTests: XCTestCase {
}
}

func writeTestSegmentedControl() {}
func testSegmentedControl() {
let segmentedControl = UISegmentedControl()
// Make sure the test is valid and the title text attributes are not defined for the normal state
XCTAssertNil(segmentedControl.titleTextAttributes(for: .normal))

segmentedControl.insertSegment(withTitle: ".", at: 0, animated: false)

// Assign the title text attributes for the normal state and ensure original values match
segmentedControl.setTitleTextAttributes(adaptiveStyle.attributes, for: .normal)

var attributes = segmentedControl.titleTextAttributes(for: .normal)
XCTAssertEqual(attributes?[.font] as? UIFont, expectedFont)
BONAssertColor(inAttributes: attributes, key: .foregroundColor, color: adaptiveStyle.color!)
BONAssert(attributes: attributes, query: { $0.pointSize }, float: expectedFont.pointSize)

// Update the trait collection and ensure the font grows.
if #available(iOS 10.0, tvOS 10.0, *) {
segmentedControl.adaptText(forTraitCollection: UITraitCollection(preferredContentSizeCategory: UIContentSizeCategory.extraLarge))
attributes = segmentedControl.titleTextAttributes(for: .normal)
BONAssert(attributes: attributes, query: { $0.pointSize }, float: expectedFont.pointSize + 2)
BONAssertColor(inAttributes: attributes, key: .foregroundColor, color: adaptiveStyle.color!)
}
}

func writeTestNavigationBar() {}
func writeTestToolbar() {}
func writeTestViewController() {}
Expand Down

0 comments on commit 99cca88

Please sign in to comment.