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

Add ability to color SegmentioCell objects based on position, not current state #125

Open
wants to merge 2 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
20 changes: 17 additions & 3 deletions Segmentio/Source/Cells/SegmentioCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class SegmentioCell: UICollectionViewCell {
var containerView: UIView?
var imageContainerView: UIView?

var overrideTextColor: UIColor?

var topConstraint: NSLayoutConstraint?
var bottomConstraint: NSLayoutConstraint?
var cellSelected = false
Expand All @@ -42,11 +44,15 @@ class SegmentioCell: UICollectionViewCell {

if style.isWithText() {
let highlightedTitleTextColor = cellSelected ? selectedState.titleTextColor
: defaultState.titleTextColor
: defaultState.titleTextColor
let highlightedTitleFont = cellSelected ? selectedState.titleFont : defaultState.titleFont

if let overrideTextColor = overrideTextColor {
segmentTitleLabel?.textColor = overrideTextColor
} else {
segmentTitleLabel?.textColor = isHighlighted ? highlightedState.titleTextColor
: highlightedTitleTextColor
}
segmentTitleLabel?.font = isHighlighted ? highlightedState.titleFont : highlightedTitleFont
}

Expand Down Expand Up @@ -139,7 +145,11 @@ class SegmentioCell: UICollectionViewCell {
let defaultState = options.states.defaultState

if style.isWithText() {
segmentTitleLabel?.textColor = selected ? selectedState.titleTextColor : defaultState.titleTextColor
if let overrideTextColor = overrideTextColor {
segmentTitleLabel?.textColor = overrideTextColor
} else {
segmentTitleLabel?.textColor = selected ? selectedState.titleTextColor : defaultState.titleTextColor
}
segmentTitleLabel?.font = selected ? selectedState.titleFont : defaultState.titleFont
segmentTitleLabel?.alpha = selected ? selectedState.titleAlpha : defaultState.titleAlpha
segmentTitleLabel?.minimumScaleFactor = 0.5
Expand Down Expand Up @@ -286,13 +296,17 @@ class SegmentioCell: UICollectionViewCell {
if style.isWithImage() {
segmentImageView?.contentMode = options.imageContentMode
segmentImageView?.image = content.image
if let imageTintColor = content.imageTintColor {
segmentImageView?.tintColor = imageTintColor
}
}

overrideTextColor = content.overrideTextColor
if style.isWithText() {
segmentTitleLabel?.textAlignment = options.labelTextAlignment
segmentTitleLabel?.numberOfLines = options.labelTextNumberOfLines
let defaultState = options.states.defaultState
segmentTitleLabel?.textColor = defaultState.titleTextColor
segmentTitleLabel?.textColor = overrideTextColor ?? defaultState.titleTextColor
segmentTitleLabel?.font = defaultState.titleFont
segmentTitleLabel?.text = content.title
segmentTitleLabel?.minimumScaleFactor = 0.5
Expand Down
6 changes: 5 additions & 1 deletion Segmentio/Source/SegmentioOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import UIKit
public struct SegmentioItem {

public var title: String?
public var overrideTextColor: UIColor?
public var image: UIImage?
public var selectedImage: UIImage?
public var imageTintColor: UIColor?
public var badgeCount: Int?
public var badgeColor: UIColor?
public var intrinsicWidth: CGFloat {
Expand All @@ -24,10 +26,12 @@ public struct SegmentioItem {
return label.intrinsicContentSize.width
}

public init(title: String?, image: UIImage?, selectedImage: UIImage? = nil) {
public init(title: String?, image: UIImage?, selectedImage: UIImage? = nil, imageTintColor: UIColor? = nil, overrideTextColor: UIColor? = nil) {
self.title = title
self.overrideTextColor = overrideTextColor
self.image = image
self.selectedImage = selectedImage ?? image
self.imageTintColor = imageTintColor
}

public mutating func addBadge(_ count: Int, color: UIColor) {
Expand Down