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

Underline Button Feature added #130

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions Example/BetterSegmentedControl.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@
TargetAttributes = {
607FACCF1AFB9204008FA782 = {
CreatedOnToolsVersion = 6.3.1;
DevelopmentTeam = 2BLC39WP62;
LastSwiftMigration = 1020;
};
607FACE41AFB9204008FA782 = {
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
23 changes: 23 additions & 0 deletions Example/BetterSegmentedControl/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Example/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: a84635a977f3d0e6850e7888935f489adb8fd939

COCOAPODS: 1.8.4
COCOAPODS: 1.9.0.beta.2
40 changes: 36 additions & 4 deletions Pod/Classes/Segments/LabelSegment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
}
}
Expand All @@ -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,
Expand All @@ -102,7 +132,9 @@ public extension LabelSegment {
normalTextColor: normalTextColor,
selectedBackgroundColor: selectedBackgroundColor,
selectedFont: selectedFont,
selectedTextColor: selectedTextColor)
selectedTextColor: selectedTextColor,
selectedUnderlineBorder: selectedUnderlineBorder,
selectedUnderlineColor: selectedUnderlineColor)
}
}

Expand Down