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

Added AttributedLabelSegment to allow NSAttributedString segments. #164

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
117 changes: 117 additions & 0 deletions Pod/Classes/Segments/AttributedLabelSegment.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
//
// AttributedLabelSegment.swift
// BetterSegmentedControl
//
// Created by Greg Gardner on 08/12/2021.
//

#if canImport(UIKit)

import UIKit

open class AttributedLabelSegment: BetterSegmentedControlSegment {
// MARK: Constants
private struct DefaultValues {
static let normalBackgroundColor: UIColor = .clear
static let normalTextColor: UIColor = .black
static let normalFont: UIFont = .systemFont(ofSize: 13)
static let selectedBackgroundColor: UIColor = .clear
static let selectedTextColor: UIColor = .black
static let selectedFont: UIFont = .systemFont(ofSize: 13, weight: .medium)
}

// MARK: Properties
public let attributedText: NSAttributedString?

public let normalFont: UIFont
public let normalTextColor: UIColor
public let normalBackgroundColor: UIColor

public let selectedFont: UIFont
public let selectedTextColor: UIColor
public let selectedBackgroundColor: UIColor

private let numberOfLines: Int
private let accessibilityIdentifier: String?

// MARK: Lifecycle
public init(attributedText: NSAttributedString? = nil,
numberOfLines: Int = 1,
normalBackgroundColor: UIColor? = nil,
normalFont: UIFont? = nil,
normalTextColor: UIColor? = nil,
selectedBackgroundColor: UIColor? = nil,
selectedFont: UIFont? = nil,
selectedTextColor: UIColor? = nil,
accessibilityIdentifier: String? = nil) {
self.attributedText = attributedText
self.numberOfLines = numberOfLines
self.normalBackgroundColor = normalBackgroundColor ?? DefaultValues.normalBackgroundColor
self.normalFont = normalFont ?? DefaultValues.normalFont
self.normalTextColor = normalTextColor ?? DefaultValues.normalTextColor
self.selectedBackgroundColor = selectedBackgroundColor ?? DefaultValues.selectedBackgroundColor
self.selectedFont = selectedFont ?? DefaultValues.selectedFont
self.selectedTextColor = selectedTextColor ?? DefaultValues.selectedTextColor
self.accessibilityIdentifier = accessibilityIdentifier
}

// MARK: BetterSegmentedControlSegment
public var intrinsicContentSize: CGSize? {
selectedView.intrinsicContentSize
}

public lazy var normalView: UIView = {
createLabel(withAttributedText: attributedText,
backgroundColor: normalBackgroundColor,
font: normalFont,
textColor: normalTextColor,
accessibilityIdentifier: accessibilityIdentifier)
}()
public lazy var selectedView: UIView = {
createLabel(withAttributedText: attributedText,
backgroundColor: selectedBackgroundColor,
font: selectedFont,
textColor: selectedTextColor,
accessibilityIdentifier: accessibilityIdentifier)
}()
open func createLabel(withAttributedText attributedText: NSAttributedString?,
backgroundColor: UIColor,
font: UIFont,
textColor: UIColor,
accessibilityIdentifier: String?) -> UILabel {
let label = UILabel()
label.attributedText = attributedText
label.numberOfLines = numberOfLines
label.backgroundColor = backgroundColor
label.font = font
label.textColor = textColor
label.lineBreakMode = .byTruncatingTail
label.textAlignment = .center
label.accessibilityIdentifier = accessibilityIdentifier
return label
}
}

public extension AttributedLabelSegment {
class func segments(withTitles titles: [NSAttributedString],
numberOfLines: Int = 1,
normalBackgroundColor: UIColor? = nil,
normalFont: UIFont? = nil,
normalTextColor: UIColor? = nil,
selectedBackgroundColor: UIColor? = nil,
selectedFont: UIFont? = nil,
selectedTextColor: UIColor? = nil) -> [BetterSegmentedControlSegment] {
titles.map {
AttributedLabelSegment(attributedText: $0,
numberOfLines: numberOfLines,
normalBackgroundColor: normalBackgroundColor,
normalFont: normalFont,
normalTextColor: normalTextColor,
selectedBackgroundColor: selectedBackgroundColor,
selectedFont: selectedFont,
selectedTextColor: selectedTextColor)
}
}
}

#endif