From e89c184552349286c7873cb6485a4ef32333a7c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Folta=CC=81nyi=20Kolos?= <> Date: Thu, 10 Oct 2019 16:18:12 +0200 Subject: [PATCH 1/4] Adds segment spacaing --- Example/BetterSegmentedControl/ViewController.swift | 1 + Pod/Classes/BetterSegmentedControl.swift | 8 ++++++-- Pod/Classes/Options.swift | 1 + 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Example/BetterSegmentedControl/ViewController.swift b/Example/BetterSegmentedControl/ViewController.swift index 1cdd018..d74f38e 100644 --- a/Example/BetterSegmentedControl/ViewController.swift +++ b/Example/BetterSegmentedControl/ViewController.swift @@ -36,6 +36,7 @@ class ViewController: UIViewController { control1.segments = LabelSegment.segments(withTitles: ["Recent", "Nearby", "All"], normalFont: UIFont(name: "HelveticaNeue-Light", size: 13.0)!, selectedFont: UIFont(name: "HelveticaNeue-Medium", size: 13.0)!) + control1.segmentSpacing = 50 // Control 2: Exclusively defined in IB // Control 3: Many options diff --git a/Pod/Classes/BetterSegmentedControl.swift b/Pod/Classes/BetterSegmentedControl.swift index c7710f8..b8b5ad6 100644 --- a/Pod/Classes/BetterSegmentedControl.swift +++ b/Pod/Classes/BetterSegmentedControl.swift @@ -100,6 +100,8 @@ import Foundation cornerRadius = value case let .bouncesOnChange(value): bouncesOnChange = value + case let .segmentSpacing(value): + segmentSpacing = value } } } @@ -173,6 +175,7 @@ import Foundation private var selectedSegments: [UIView] { return selectedSegmentsView.subviews } private var segmentViews: [UIView] { return normalSegments + selectedSegments } private var totalInsetSize: CGFloat { return indicatorViewInset * 2.0 } + private var totalSpacings: CGFloat { return segmentSpacing * CGFloat(normalSegmentCount - 1) } private lazy var defaultSegments: [BetterSegmentedControlSegment] = { return [LabelSegment(text: "First"), LabelSegment(text: "Second")] }() @@ -327,8 +330,9 @@ import Foundation // MARK: Helpers private func elementFrame(forIndex index: Int) -> CGRect { - let elementWidth = (width - totalInsetSize) / CGFloat(normalSegmentCount) - let x = CGFloat(isLayoutDirectionRightToLeft ? lastIndex - index : index) * elementWidth + let elementWidth = (width - totalInsetSize - totalSpacings) / CGFloat(normalSegmentCount) + let spacingOffset = CGFloat(index) * segmentSpacing + let x = CGFloat(isLayoutDirectionRightToLeft ? lastIndex - index : index) * elementWidth + spacingOffset return CGRect(x: x + indicatorViewInset, y: indicatorViewInset, width: elementWidth, diff --git a/Pod/Classes/Options.swift b/Pod/Classes/Options.swift index acad9b9..e8eb677 100644 --- a/Pod/Classes/Options.swift +++ b/Pod/Classes/Options.swift @@ -24,4 +24,5 @@ public enum BetterSegmentedControlOption { case backgroundColor(UIColor) case cornerRadius(CGFloat) case bouncesOnChange(Bool) + case segmentSpacing(CGFloat) } From 335c1c0371ffb8a2ba70ddb2071fa3bbf5bea286 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Folta=CC=81nyi=20Kolos?= <> Date: Thu, 10 Oct 2019 16:47:57 +0200 Subject: [PATCH 2/4] Adds segmentSpacing property --- Pod/Classes/BetterSegmentedControl.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Pod/Classes/BetterSegmentedControl.swift b/Pod/Classes/BetterSegmentedControl.swift index b8b5ad6..aa2b99f 100644 --- a/Pod/Classes/BetterSegmentedControl.swift +++ b/Pod/Classes/BetterSegmentedControl.swift @@ -159,6 +159,10 @@ import Foundation indicatorView.layer.borderColor = newValue?.cgColor } } + /// The horizontal spacing between segments. + @IBInspectable public var segmentSpacing: CGFloat = 0 { + didSet { setNeedsLayout() } + } // MARK: Private properties private let normalSegmentsView = UIView() @@ -175,7 +179,7 @@ import Foundation private var selectedSegments: [UIView] { return selectedSegmentsView.subviews } private var segmentViews: [UIView] { return normalSegments + selectedSegments } private var totalInsetSize: CGFloat { return indicatorViewInset * 2.0 } - private var totalSpacings: CGFloat { return segmentSpacing * CGFloat(normalSegmentCount - 1) } + private var totalSpacings: CGFloat { return segmentSpacing * CGFloat(normalSegmentCount - 1) } private lazy var defaultSegments: [BetterSegmentedControlSegment] = { return [LabelSegment(text: "First"), LabelSegment(text: "Second")] }() From a42ebe9c8b1cfd9ffc6dff8fafe6d43bdc14f48b Mon Sep 17 00:00:00 2001 From: Paul Lee Date: Fri, 30 Apr 2021 09:41:38 +0800 Subject: [PATCH 3/4] install pods --- Example/Podfile.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Example/Podfile.lock b/Example/Podfile.lock index b60d72d..edf310f 100644 --- a/Example/Podfile.lock +++ b/Example/Podfile.lock @@ -1,5 +1,5 @@ PODS: - - BetterSegmentedControl (2.0) + - BetterSegmentedControl (2.0.1) - iOSSnapshotTestCase (5.0.2): - iOSSnapshotTestCase/SwiftSupport (= 5.0.2) - iOSSnapshotTestCase/Core (5.0.2) @@ -31,7 +31,7 @@ EXTERNAL SOURCES: :path: "../" SPEC CHECKSUMS: - BetterSegmentedControl: ce9b68b81c963991211c561026ed2cf711f9ac63 + BetterSegmentedControl: 09607b27861d49cbce48b7673b74f9150a3d371a iOSSnapshotTestCase: 2d51aa06775e95cecb0a1fb9c5c159ccd1dd4596 Nimble: 051e3d8912d40138fa5591c78594f95fb172af37 Nimble-Snapshots: bbd1ab264bacc24a9ce24a8363bc05aac783aeb0 @@ -39,4 +39,4 @@ SPEC CHECKSUMS: PODFILE CHECKSUM: 609c5442f2ad21fe0424eaab055f9caa5c2944a1 -COCOAPODS: 1.9.3 +COCOAPODS: 1.10.0 From 5ef25d15b58d1cb8aa67b7f040b4dbfc83fb4cd2 Mon Sep 17 00:00:00 2001 From: Paul Lee Date: Fri, 30 Apr 2021 11:08:35 +0800 Subject: [PATCH 4/4] Adding `normalAttributes` and `selectedAttributes` to `LabelSegment` to support displaying attributedString. --- Pod/Classes/Segments/LabelSegment.swift | 31 +++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/Pod/Classes/Segments/LabelSegment.swift b/Pod/Classes/Segments/LabelSegment.swift index f32f64c..c83df98 100644 --- a/Pod/Classes/Segments/LabelSegment.swift +++ b/Pod/Classes/Segments/LabelSegment.swift @@ -15,9 +15,22 @@ open class LabelSegment: BetterSegmentedControlSegment { static let normalBackgroundColor: UIColor = .clear static let normalTextColor: UIColor = .black static let normalFont: UIFont = .systemFont(ofSize: 13) + static var normalAttributes: ((UIFont?, UIColor?) -> [NSAttributedString.Key: Any]) = { (normalFont, normalTextColor) in + var attrs = [NSAttributedString.Key: Any]() + attrs[NSAttributedString.Key.font] = normalFont == nil ? DefaultValues.normalFont : normalFont! + attrs[NSAttributedString.Key.foregroundColor] = normalTextColor == nil ? DefaultValues.normalTextColor : normalTextColor! + return attrs + } + static let selectedBackgroundColor: UIColor = .clear static let selectedTextColor: UIColor = .black static let selectedFont: UIFont = .systemFont(ofSize: 13, weight: .medium) + static var selectedAttributes: ((UIFont?, UIColor?) -> [NSAttributedString.Key: Any]) = { (selectedFont, selectedTextColor) in + var attrs = [NSAttributedString.Key: Any]() + attrs[NSAttributedString.Key.font] = selectedFont == nil ? DefaultValues.selectedFont : selectedFont! + attrs[NSAttributedString.Key.foregroundColor] = selectedTextColor == nil ? DefaultValues.selectedTextColor : selectedTextColor! + return attrs + } } // MARK: Properties @@ -26,10 +39,12 @@ open class LabelSegment: BetterSegmentedControlSegment { public let normalFont: UIFont public let normalTextColor: UIColor public let normalBackgroundColor: UIColor + public let normalAttributes: [NSAttributedString.Key: Any] public let selectedFont: UIFont public let selectedTextColor: UIColor public let selectedBackgroundColor: UIColor + public let selectedAttributes: [NSAttributedString.Key: Any] private let numberOfLines: Int private let accessibilityIdentifier: String? @@ -40,18 +55,22 @@ open class LabelSegment: BetterSegmentedControlSegment { normalBackgroundColor: UIColor? = nil, normalFont: UIFont? = nil, normalTextColor: UIColor? = nil, + normalAttributes: [NSAttributedString.Key: Any]? = nil, selectedBackgroundColor: UIColor? = nil, selectedFont: UIFont? = nil, selectedTextColor: UIColor? = nil, + selectedAttributes: [NSAttributedString.Key: Any]? = nil, accessibilityIdentifier: String? = nil) { self.text = text self.numberOfLines = numberOfLines self.normalBackgroundColor = normalBackgroundColor ?? DefaultValues.normalBackgroundColor self.normalFont = normalFont ?? DefaultValues.normalFont self.normalTextColor = normalTextColor ?? DefaultValues.normalTextColor + self.normalAttributes = normalAttributes ?? DefaultValues.normalAttributes(normalFont, normalTextColor) self.selectedBackgroundColor = selectedBackgroundColor ?? DefaultValues.selectedBackgroundColor self.selectedFont = selectedFont ?? DefaultValues.selectedFont self.selectedTextColor = selectedTextColor ?? DefaultValues.selectedTextColor + self.selectedAttributes = selectedAttributes ?? DefaultValues.selectedAttributes(selectedFont, selectedTextColor) self.accessibilityIdentifier = accessibilityIdentifier } @@ -65,6 +84,7 @@ open class LabelSegment: BetterSegmentedControlSegment { backgroundColor: normalBackgroundColor, font: normalFont, textColor: normalTextColor, + attributes: normalAttributes, accessibilityIdentifier: accessibilityIdentifier) }() public lazy var selectedView: UIView = { @@ -72,12 +92,14 @@ open class LabelSegment: BetterSegmentedControlSegment { backgroundColor: selectedBackgroundColor, font: selectedFont, textColor: selectedTextColor, + attributes: selectedAttributes, accessibilityIdentifier: accessibilityIdentifier) }() open func createLabel(withText text: String?, backgroundColor: UIColor, font: UIFont, textColor: UIColor, + attributes: [NSAttributedString.Key: Any], accessibilityIdentifier: String?) -> UILabel { let label = UILabel() label.text = text @@ -85,6 +107,7 @@ open class LabelSegment: BetterSegmentedControlSegment { label.backgroundColor = backgroundColor label.font = font label.textColor = textColor + label.attributedText = text == nil ? nil : NSAttributedString(string: text!, attributes: attributes) label.lineBreakMode = .byTruncatingTail label.textAlignment = .center label.accessibilityIdentifier = accessibilityIdentifier @@ -98,18 +121,22 @@ public extension LabelSegment { normalBackgroundColor: UIColor? = nil, normalFont: UIFont? = nil, normalTextColor: UIColor? = nil, + normalAttributes: [NSAttributedString.Key: Any]? = nil, selectedBackgroundColor: UIColor? = nil, selectedFont: UIFont? = nil, - selectedTextColor: UIColor? = nil) -> [BetterSegmentedControlSegment] { + selectedTextColor: UIColor? = nil, + selectedAttributes: [NSAttributedString.Key: Any]? = nil) -> [BetterSegmentedControlSegment] { titles.map { LabelSegment(text: $0, numberOfLines: numberOfLines, normalBackgroundColor: normalBackgroundColor, normalFont: normalFont, normalTextColor: normalTextColor, + normalAttributes: normalAttributes, selectedBackgroundColor: selectedBackgroundColor, selectedFont: selectedFont, - selectedTextColor: selectedTextColor) + selectedTextColor: selectedTextColor, + selectedAttributes: selectedAttributes) } } }