diff --git a/Example/BetterSegmentedControl.xcodeproj/project.pbxproj b/Example/BetterSegmentedControl.xcodeproj/project.pbxproj index 4b67021..561b22c 100644 --- a/Example/BetterSegmentedControl.xcodeproj/project.pbxproj +++ b/Example/BetterSegmentedControl.xcodeproj/project.pbxproj @@ -219,6 +219,7 @@ TargetAttributes = { 607FACCF1AFB9204008FA782 = { CreatedOnToolsVersion = 6.3.1; + DevelopmentTeam = 2BLC39WP62; LastSwiftMigration = 1020; }; 607FACE41AFB9204008FA782 = { @@ -513,6 +514,7 @@ baseConfigurationReference = E35EF8EE3A68B8AA955DE5DE /* Pods-BetterSegmentedControl_Example.debug.xcconfig */; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + DEVELOPMENT_TEAM = 2BLC39WP62; INFOPLIST_FILE = BetterSegmentedControl/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; MODULE_NAME = ExampleApp; @@ -526,6 +528,7 @@ baseConfigurationReference = 12CB0D1475C52C17775132BF /* Pods-BetterSegmentedControl_Example.release.xcconfig */; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + DEVELOPMENT_TEAM = 2BLC39WP62; INFOPLIST_FILE = BetterSegmentedControl/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; MODULE_NAME = ExampleApp; diff --git a/Example/BetterSegmentedControl/ViewController.swift b/Example/BetterSegmentedControl/ViewController.swift index deb213d..8f413ec 100644 --- a/Example/BetterSegmentedControl/ViewController.swift +++ b/Example/BetterSegmentedControl/ViewController.swift @@ -83,6 +83,29 @@ class ViewController: UIViewController { height: 32.0), titles: ["First", "Second", "Third"]) view.addSubview(appleStyledControl) + + // Control 7: Added as a Underline button + let underlineSegmentedControl = BetterSegmentedControl( + frame: CGRect(x: 0.0, y: 332.0, width: view.bounds.width , height: 50.0), + segments: LabelSegment.segments(withTitles: ["Artists", "Albums", "Feature"], + normalFont: UIFont(name: "HelveticaNeue", size: 16.0)!, + normalTextColor: .gray, + selectedFont: UIFont(name: "HelveticaNeue", size: 16.0)!, + selectedTextColor: UIColor(red: 0.97, green: 0.00, blue: 0.24, alpha: 1.00), + selectedUnderlineBorder: 5.0, + selectedUnderlineColor: UIColor.purple), + index: 1, + options: [.backgroundColor(UIColor.white), + .backgroundColor(UIColor.white), + .indicatorViewBackgroundColor(UIColor.white), + .indicatorViewBorderColor(UIColor.red), + .indicatorViewInset(0.0), + .cornerRadius(0.0), + .animationSpringDamping(1.0), + .panningDisabled(true)]) + + + view.addSubview(underlineSegmentedControl) } // MARK: - Action handlers diff --git a/Example/Podfile.lock b/Example/Podfile.lock index 96962ea..ea2c304 100644 --- a/Example/Podfile.lock +++ b/Example/Podfile.lock @@ -39,4 +39,4 @@ SPEC CHECKSUMS: PODFILE CHECKSUM: a84635a977f3d0e6850e7888935f489adb8fd939 -COCOAPODS: 1.8.4 +COCOAPODS: 1.9.0.beta.2 diff --git a/Pod/Classes/Segments/LabelSegment.swift b/Pod/Classes/Segments/LabelSegment.swift index 226f9b8..019462a 100644 --- a/Pod/Classes/Segments/LabelSegment.swift +++ b/Pod/Classes/Segments/LabelSegment.swift @@ -27,6 +27,9 @@ open class LabelSegment: BetterSegmentedControlSegment { public let selectedFont: UIFont public let selectedTextColor: UIColor public let selectedBackgroundColor: UIColor + public let selectedUnderlineView: UIImageView + public let selectedUnderlineBorder: CGFloat + public let selectedUnderlineColor: UIColor private let numberOfLines: Int private let accessibilityIdentifier: String? @@ -40,6 +43,8 @@ open class LabelSegment: BetterSegmentedControlSegment { selectedBackgroundColor: UIColor? = nil, selectedFont: UIFont? = nil, selectedTextColor: UIColor? = nil, + selectedUnderlineBorder: CGFloat = 0, + selectedUnderlineColor: UIColor? = nil, accessibilityIdentifier: String? = nil) { self.text = text self.numberOfLines = numberOfLines @@ -50,6 +55,20 @@ open class LabelSegment: BetterSegmentedControlSegment { self.selectedFont = selectedFont ?? DefaultValues.font self.selectedTextColor = selectedTextColor ?? DefaultValues.selectedTextColor self.accessibilityIdentifier = accessibilityIdentifier + self.selectedUnderlineView = UIImageView() + self.selectedUnderlineBorder = selectedUnderlineBorder + self.selectedUnderlineColor = selectedUnderlineColor ?? DefaultValues.selectedBackgroundColor + } + + private func updateUnderline(border:CGFloat, color:UIColor?, target:UIView) { + target.addSubview(selectedUnderlineView) + selectedUnderlineView.backgroundColor = color! + + selectedUnderlineView.translatesAutoresizingMaskIntoConstraints = false + selectedUnderlineView.leftAnchor.constraint(equalTo: target.leftAnchor).isActive = true + selectedUnderlineView.rightAnchor.constraint(equalTo: target.rightAnchor).isActive = true + selectedUnderlineView.bottomAnchor.constraint(equalTo: target.bottomAnchor).isActive = true + selectedUnderlineView.heightAnchor.constraint(equalToConstant: border).isActive = true } // MARK: BetterSegmentedControlSegment @@ -65,13 +84,17 @@ open class LabelSegment: BetterSegmentedControlSegment { backgroundColor: selectedBackgroundColor, font: selectedFont, textColor: selectedTextColor, - accessibilityIdentifier: accessibilityIdentifier) + accessibilityIdentifier: accessibilityIdentifier, + underlineBorder: selectedUnderlineBorder, + underlineColor: selectedUnderlineColor) }() open func createLabel(withText text: String?, backgroundColor: UIColor, font: UIFont, textColor: UIColor, - accessibilityIdentifier: String?) -> UILabel { + accessibilityIdentifier: String?, + underlineBorder:CGFloat = 0, + underlineColor:UIColor? = nil) -> UILabel { let label = UILabel() label.text = text label.numberOfLines = numberOfLines @@ -81,6 +104,11 @@ open class LabelSegment: BetterSegmentedControlSegment { label.lineBreakMode = .byTruncatingTail label.textAlignment = .center label.accessibilityIdentifier = accessibilityIdentifier + + if underlineBorder > 0 { + updateUnderline(border: underlineBorder, color:underlineColor, target: label) + } + return label } } @@ -93,7 +121,9 @@ public extension LabelSegment { normalTextColor: UIColor? = nil, selectedBackgroundColor: UIColor? = nil, selectedFont: UIFont? = nil, - selectedTextColor: UIColor? = nil) -> [BetterSegmentedControlSegment] { + selectedTextColor: UIColor? = nil, + selectedUnderlineBorder: CGFloat = 0, + selectedUnderlineColor: UIColor? = nil) -> [BetterSegmentedControlSegment] { return titles.map { LabelSegment(text: $0, numberOfLines: numberOfLines, @@ -102,7 +132,9 @@ public extension LabelSegment { normalTextColor: normalTextColor, selectedBackgroundColor: selectedBackgroundColor, selectedFont: selectedFont, - selectedTextColor: selectedTextColor) + selectedTextColor: selectedTextColor, + selectedUnderlineBorder: selectedUnderlineBorder, + selectedUnderlineColor: selectedUnderlineColor) } }