Skip to content

Commit

Permalink
Added selectedTitleFont property. Updated example to showcase it. Rem…
Browse files Browse the repository at this point in the history
…oved now irrelevant allTitleLabels property. Updated documentation. Added tests for untested public properties.
  • Loading branch information
gmarm committed Jun 26, 2016
1 parent 3f530dc commit 1c25145
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 37 deletions.
4 changes: 4 additions & 0 deletions Example/BetterSegmentedControl/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ class ViewController: UIViewController {
selectedTitleColor: .whiteColor())
navigationSegmentedControl.cornerRadius = 3.0
navigationSegmentedControl.titleFont = UIFont(name: "Avenir", size: 13.0)!
navigationSegmentedControl.selectedTitleFont = UIFont(name: "Avenir", size: 13.0)!
navigationSegmentedControl.bouncesOnChange = false
navigationSegmentedControl.addTarget(self, action: #selector(ViewController.navigationSegmentedControlValueChanged(_:)), forControlEvents: .ValueChanged)
navigationItem.titleView = navigationSegmentedControl

// Control 1: Created and designed in IB that announces its value on interaction
control1.titles = ["Recent","Nearby","All"]
control1.titleFont = UIFont(name: "HelveticaNeue-Medium", size: 13.0)!
control1.selectedTitleFont = UIFont(name: "HelveticaNeue-Medium", size: 13.0)!
control1.alwaysAnnouncesValue = true
print(control1.titles)

Expand All @@ -44,6 +46,7 @@ class ViewController: UIViewController {
// Control 3: Many options & error handling
control3.titles = ["One","Two","Three","Four","Five","Six"]
control3.titleFont = UIFont(name: "HelveticaNeue-Light", size: 14.0)!
control3.selectedTitleFont = UIFont(name: "HelveticaNeue-Medium", size: 14.0)!
do {
try control3.setIndex(10, animated: false)
}
Expand All @@ -67,6 +70,7 @@ class ViewController: UIViewController {
viewSegmentedControl.autoresizingMask = [.FlexibleWidth]
viewSegmentedControl.cornerRadius = 0.0
viewSegmentedControl.titleFont = UIFont(name: "HelveticaNeue", size: 16.0)!
viewSegmentedControl.selectedTitleFont = UIFont(name: "HelveticaNeue", size: 16.0)!
viewSegmentedControl.bouncesOnChange = false
viewSegmentedControl.panningDisabled = true
view.addSubview(viewSegmentedControl)
Expand Down
175 changes: 145 additions & 30 deletions Example/Tests/Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Nimble
class BetterSegmentedControlSpec: QuickSpec {
override func spec() {
describe("a BetterSegmentedControl") {
context("when it is initialized") {
context("after it is initialized") {
context("using the designated initializer") {
var control: BetterSegmentedControl!
beforeEach({
Expand All @@ -27,32 +27,90 @@ class BetterSegmentedControlSpec: QuickSpec {
selectedTitleColor: .purpleColor())
})
it("has the frame passed", closure: {
let frame = control.frame
expect(CGRectEqualToRect(frame,CGRectMake(0, 0, 300, 44))).to(beTrue())
expect(CGRectEqualToRect(control.frame,CGRectMake(0, 0, 300, 44))).to(beTrue())
})
it("has the index passed", closure: {
let index = control.index
expect(index) == 1
expect(control.index) == 1
})
it("has the titles passed", closure: {
let titles = control.titles
expect(titles) == ["One","Two"]
expect(control.titles) == ["One","Two"]
})
it("has the background color passed", closure: {
let color = control.backgroundColor
expect(color).to(equal(UIColor.redColor()))
expect(control.backgroundColor).to(equal(UIColor.redColor()))
})
it("has the title color passed", closure: {
let color = control.titleColor
expect(color).to(equal(UIColor.blueColor()))
expect(control.titleColor).to(equal(UIColor.blueColor()))
})
it("has the indicator view background color passed", closure: {
let color = control.indicatorViewBackgroundColor
expect(color).to(equal(UIColor.greenColor()))
expect(control.indicatorViewBackgroundColor).to(equal(UIColor.greenColor()))
})
it("has the selected title color passed", closure: {
let color = control.selectedTitleColor
expect(color).to(equal(UIColor.purpleColor()))
expect(control.selectedTitleColor).to(equal(UIColor.purpleColor()))
})
describe("its bouncesOnChange property", closure: {
it("defaults to true", closure: {
expect(control.bouncesOnChange).to(beTrue())
})
it("can be set", closure: {
control.bouncesOnChange = false
expect(control.bouncesOnChange).to(beFalse())
})
})
describe("its alwaysAnnouncesValue property", closure: {
it("defaults to false", closure: {
expect(control.alwaysAnnouncesValue).to(beFalse())
})
it("can be set", closure: {
control.alwaysAnnouncesValue = true
expect(control.alwaysAnnouncesValue).to(beTrue())
})
})
describe("its panningDisabled property", closure: {
it("defaults to false", closure: {
expect(control.panningDisabled).to(beFalse())
})
it("can be set", closure: {
control.panningDisabled = true
expect(control.panningDisabled).to(beTrue())
})
})
describe("its cornerRadius property", closure: {
it("defaults to 0", closure: {
expect(control.cornerRadius).to(equal(0.0))
})
it("can be set", closure: {
control.cornerRadius = 10.0
expect(control.cornerRadius).to(equal(10.0))
})
})
describe("its indicatorViewInset property", closure: {
it("defaults to 2.0", closure: {
expect(control.indicatorViewInset).to(equal(2.0))
})
it("can be set", closure: {
control.indicatorViewInset = 4.0
expect(control.indicatorViewInset).to(equal(4.0))
})
})
describe("its titleFont property", closure: {
it("defaults to the default UILabel font", closure: {
expect(control.titleFont).to(equal(UILabel().font))
})
it("can be set", closure: {
let newFont = UIFont(name: "HelveticaNeue-Light", size: 14.0)!
control.titleFont = newFont
expect(control.titleFont).to(equal(newFont))
})
})
describe("its selectedTitleFont property", closure: {
it("defaults to the default UILabel font", closure: {
expect(control.selectedTitleFont).to(equal(UILabel().font))
})
it("can be set", closure: {
let newFont = UIFont(name: "HelveticaNeue-Light", size: 14.0)!
control.selectedTitleFont = newFont
expect(control.selectedTitleFont).to(equal(newFont))
})
})
}
context("using initWithCoder") {
Expand All @@ -64,39 +122,96 @@ class BetterSegmentedControlSpec: QuickSpec {
UIApplication.sharedApplication().keyWindow!.rootViewController = viewController
expect(viewController).toNot(beNil())
expect(viewController.view).toNot(beNil())
expect(viewController.control).toNot(beNil())
control = viewController.control
})
it("is not nil", closure: {
expect(control).toNot(beNil())
})
it("has the frame set in IB", closure: {
let frame = control.frame
expect(CGRectEqualToRect(frame,CGRectMake(10, 30, 480, 40))).to(beTrue())
expect(CGRectEqualToRect(control.frame,CGRectMake(10, 30, 480, 40))).to(beTrue())
})
it("has the default index 0", closure: {
let index = control.index
expect(index) == 0
expect(control.index) == 0
})
it("has the default titles 'First, Second'", closure: {
let titles = control.titles
expect(titles) == ["First","Second"]
expect(control.titles) == ["First","Second"]
})
it("has the background color set in IB", closure: {
let color = control.backgroundColor
expect(color).to(equal(UIColor(red: 0, green: 0, blue: 0, alpha: 1)))
expect(control.backgroundColor).to(equal(UIColor(red: 0, green: 0, blue: 0, alpha: 1)))
})
it("has the title color set in IB", closure: {
let color = control.titleColor
expect(color).to(equal(UIColor(red: 1, green: 1, blue: 1, alpha: 1)))
expect(control.titleColor).to(equal(UIColor(red: 1, green: 1, blue: 1, alpha: 1)))
})
it("has the indicator view background color set in IB", closure: {
let color = control.indicatorViewBackgroundColor
expect(color).to(equal(UIColor(red: 1, green: 1, blue: 1, alpha: 1)))
expect(control.indicatorViewBackgroundColor).to(equal(UIColor(red: 1, green: 1, blue: 1, alpha: 1)))
})
it("has the selected title color set in IB", closure: {
let color = control.selectedTitleColor
expect(color).to(equal(UIColor(red: 0, green: 0, blue: 0, alpha: 1)))
expect(control.selectedTitleColor).to(equal(UIColor(red: 0, green: 0, blue: 0, alpha: 1)))
})
describe("its bouncesOnChange property", closure: {
it("defaults to true", closure: {
expect(control.bouncesOnChange).to(beTrue())
})
it("can be set", closure: {
control.bouncesOnChange = false
expect(control.bouncesOnChange).to(beFalse())
})
})
describe("its alwaysAnnouncesValue property", closure: {
it("defaults to false", closure: {
expect(control.alwaysAnnouncesValue).to(beFalse())
})
it("can be set", closure: {
control.alwaysAnnouncesValue = true
expect(control.alwaysAnnouncesValue).to(beTrue())
})
})
describe("its panningDisabled property", closure: {
it("defaults to false", closure: {
expect(control.panningDisabled).to(beFalse())
})
it("can be set", closure: {
control.panningDisabled = true
expect(control.panningDisabled).to(beTrue())
})
})
describe("its cornerRadius property", closure: {
it("has the value set in IB", closure: {
expect(control.cornerRadius).to(equal(20.0))
})
it("can be set", closure: {
control.cornerRadius = 10.0
expect(control.cornerRadius).to(equal(10.0))
})
})
describe("its indicatorViewInset property", closure: {
it("defaults to 2.0", closure: {
expect(control.indicatorViewInset).to(equal(2.0))
})
it("can be set", closure: {
control.indicatorViewInset = 4.0
expect(control.indicatorViewInset).to(equal(4.0))
})
})
describe("its titleFont property", closure: {
it("defaults to the default UILabel font", closure: {
expect(control.titleFont).to(equal(UILabel().font))
})
it("can be set", closure: {
let newFont = UIFont(name: "HelveticaNeue-Light", size: 14.0)!
control.titleFont = newFont
expect(control.titleFont).to(equal(newFont))
})
})
describe("its selectedTitleFont property", closure: {
it("defaults to the default UILabel font", closure: {
expect(control.selectedTitleFont).to(equal(UILabel().font))
})
it("can be set", closure: {
let newFont = UIFont(name: "HelveticaNeue-Light", size: 14.0)!
control.selectedTitleFont = newFont
expect(control.selectedTitleFont).to(equal(newFont))
})
})
}
}
Expand Down
23 changes: 16 additions & 7 deletions Pod/Classes/BetterSegmentedControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ import UIKit
selectedTitleLabel.text = string
selectedTitleLabel.lineBreakMode = .ByTruncatingTail
selectedTitleLabel.textAlignment = .Center
selectedTitleLabel.font = titleFont
selectedTitleLabel.font = selectedTitleFont

return (titleLabel, selectedTitleLabel)
}
Expand All @@ -108,9 +108,9 @@ import UIKit
}
/// Whether the indicator should bounce when selecting a new index. Defaults to true.
public var bouncesOnChange = true
/// Whether the the control should always send the .ValueChanged event, regardless of the index remaining the same after interaction. Defaults to false.
/// Whether the the control should always send the .ValueChanged event, regardless of the index remaining unchanged after interaction. Defaults to false.
public var alwaysAnnouncesValue = false
/// Whether the the control should respond to pan gestures. Defaults to false.
/// Whether the the control should ignore pan gestures. Defaults to false.
public var panningDisabled = false
/// The control's and indicator's corner radii
@IBInspectable public var cornerRadius: CGFloat {
Expand All @@ -129,7 +129,7 @@ import UIKit
}
set { indicatorView.backgroundColor = newValue }
}
/// The indicator view's inset
/// The indicator view's inset. Defaults to 2.0.
@IBInspectable public var indicatorViewInset: CGFloat = 2.0 {
didSet { setNeedsLayout() }
}
Expand All @@ -156,13 +156,23 @@ import UIKit
/// The titles' font
public var titleFont: UIFont = UILabel().font {
didSet {
if !allTitleLabels.isEmpty {
for label in allTitleLabels {
if !titleLabels.isEmpty {
for label in titleLabels {
label.font = titleFont
}
}
}
}
/// The selected title's font
public var selectedTitleFont: UIFont = UILabel().font {
didSet {
if !selectedTitleLabels.isEmpty {
for label in selectedTitleLabels {
label.font = selectedTitleFont
}
}
}
}

// MARK: - Private properties
private let titleLabelsView = UIView()
Expand All @@ -178,7 +188,6 @@ import UIKit
private var titleLabelsCount: Int { return titleLabelsView.subviews.count }
private var titleLabels: [UILabel] { return titleLabelsView.subviews as! [UILabel] }
private var selectedTitleLabels: [UILabel] { return selectedTitleLabelsView.subviews as! [UILabel] }
private var allTitleLabels: [UILabel] { return titleLabels + selectedTitleLabels }
private var totalInsetSize: CGFloat { return indicatorViewInset * 2.0 }
private lazy var defaultTitles: [String] = { return ["First", "Second"] }()

Expand Down

0 comments on commit 1c25145

Please sign in to comment.