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

Support text attributes for LabelSegment #159

Open
wants to merge 6 commits 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
1 change: 1 addition & 0 deletions Example/BetterSegmentedControl/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class ViewController: UIViewController {
// Control 1: Created and designed in IB
control1.segments = LabelSegment.segments(withTitles: ["Recent", "Nearby", "All"],
normalTextColor: UIColor(red: 0.48, green: 0.48, blue: 0.51, alpha: 1.00))
control1.segmentSpacing = 50

// Control 2: Created and designed in IB
control2.segments = LabelSegment.segments(withTitles: ["Music", "Movies", "Apps"],
Expand Down
6 changes: 3 additions & 3 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -31,12 +31,12 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
BetterSegmentedControl: ce9b68b81c963991211c561026ed2cf711f9ac63
BetterSegmentedControl: 09607b27861d49cbce48b7673b74f9150a3d371a
iOSSnapshotTestCase: 2d51aa06775e95cecb0a1fb9c5c159ccd1dd4596
Nimble: 051e3d8912d40138fa5591c78594f95fb172af37
Nimble-Snapshots: bbd1ab264bacc24a9ce24a8363bc05aac783aeb0
Quick: 7fb19e13be07b5dfb3b90d4f9824c855a11af40e

PODFILE CHECKSUM: 609c5442f2ad21fe0424eaab055f9caa5c2944a1

COCOAPODS: 1.9.3
COCOAPODS: 1.10.0
1 change: 1 addition & 0 deletions Pod/Classes/BetterSegmentedControl+Options.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public extension BetterSegmentedControl {
/* Other */
case backgroundColor(UIColor)
case cornerRadius(CGFloat)
case segmentSpacing(CGFloat)
}
}

Expand Down
15 changes: 12 additions & 3 deletions Pod/Classes/BetterSegmentedControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ import UIKit
indicatorView.layer.borderColor = newValue?.cgColor
}
}
/// The horizontal spacing between segments.
@IBInspectable public var segmentSpacing: CGFloat = 0 {
didSet { setNeedsLayout() }
}

/// The duration of the animation of an index change. Defaults to `0.3`.
@IBInspectable public var animationDuration: TimeInterval = 0.3
Expand Down Expand Up @@ -158,6 +162,8 @@ import UIKit

private var totalInsetSize: CGFloat { indicatorViewInset * 2.0 }

private var totalSpacings: CGFloat { return segmentSpacing * CGFloat(normalSegmentViewCount - 1) }

private var isLayoutDirectionRightToLeft: Bool {
let layoutDirection = UIView.userInterfaceLayoutDirection(for: semanticContentAttribute)
return layoutDirection == .rightToLeft
Expand Down Expand Up @@ -339,6 +345,8 @@ import UIKit
animationDuration = value
case let .animationSpringDamping(value):
animationSpringDamping = value
case let .segmentSpacing(value):
segmentSpacing = value
}
}
}
Expand Down Expand Up @@ -447,9 +455,10 @@ import UIKit
}

private func frameForElement(atIndex index: Int) -> CGRect {
let elementWidth = (width - totalInsetSize) / CGFloat(normalSegmentViewCount)
let x = CGFloat(isLayoutDirectionRightToLeft ? lastIndex - index : index) * elementWidth

let elementWidth = (width - totalInsetSize - totalSpacings) / CGFloat(normalSegmentViewCount)
let spacingOffset = CGFloat(index) * segmentSpacing
let x = CGFloat(isLayoutDirectionRightToLeft ? lastIndex - index : index) * elementWidth + spacingOffset

return CGRect(x: x + indicatorViewInset,
y: indicatorViewInset,
width: elementWidth,
Expand Down
28 changes: 28 additions & 0 deletions Pod/Classes/Options.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// Options.swift
// Pods
//
// Created by George Marmaridis on 15/05/2017.
//
//

import Foundation

public enum BetterSegmentedControlOption {
/* Selected segment */
case indicatorViewBackgroundColor(UIColor)
case indicatorViewInset(CGFloat)
case indicatorViewBorderWidth(CGFloat)
case indicatorViewBorderColor(UIColor)

/* Behavior */
case alwaysAnnouncesValue(Bool)
case announcesValueImmediately(Bool)
case panningDisabled(Bool)

/* Other */
case backgroundColor(UIColor)
case cornerRadius(CGFloat)
case bouncesOnChange(Bool)
case segmentSpacing(CGFloat)
}
31 changes: 29 additions & 2 deletions Pod/Classes/Segments/LabelSegment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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?
Expand All @@ -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
}

Expand All @@ -65,26 +84,30 @@ open class LabelSegment: BetterSegmentedControlSegment {
backgroundColor: normalBackgroundColor,
font: normalFont,
textColor: normalTextColor,
attributes: normalAttributes,
accessibilityIdentifier: accessibilityIdentifier)
}()
public lazy var selectedView: UIView = {
createLabel(withText: text,
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
label.numberOfLines = numberOfLines
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
Expand All @@ -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)
}
}
}
Expand Down