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 NSAttributedString to the SegmentioItems #117

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
16 changes: 9 additions & 7 deletions Segmentio/Source/Cells/SegmentioCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class SegmentioCell: UICollectionViewCell {
if let imageContainerView = imageContainerView {
contentView.addSubview(imageContainerView)
}

segmentImageView = UIImageView(frame: CGRect.zero)
if let segmentImageView = segmentImageView, let imageContainerView = imageContainerView {
imageContainerView.addSubview(segmentImageView)
Expand All @@ -79,7 +79,7 @@ class SegmentioCell: UICollectionViewCell {
if let segmentTitleLabel = segmentTitleLabel, let containerView = containerView {
containerView.addSubview(segmentTitleLabel)
}

segmentImageView?.translatesAutoresizingMaskIntoConstraints = false
segmentTitleLabel?.translatesAutoresizingMaskIntoConstraints = false
containerView?.translatesAutoresizingMaskIntoConstraints = false
Expand Down Expand Up @@ -144,7 +144,7 @@ class SegmentioCell: UICollectionViewCell {
segmentTitleLabel?.minimumScaleFactor = 0.5
segmentTitleLabel?.adjustsFontSizeToFitWidth = true
}

if (style != .onlyLabel) {
segmentImageView?.image = selected ? selectedImage : image
}
Expand Down Expand Up @@ -177,7 +177,7 @@ class SegmentioCell: UICollectionViewCell {
setupImageContainerConstraints()
return // implement in subclasses
}

// MARK: - Private functions

fileprivate func setupContainerConstraints() {
Expand Down Expand Up @@ -217,7 +217,7 @@ class SegmentioCell: UICollectionViewCell {
segmentTitleLabelTrailingConstraint,
segmentTitleLabelVerticalCenterConstraint,
segmentTitleLabelLeadingConstraint
])
])
}

fileprivate func setupImageContainerConstraints() {
Expand Down Expand Up @@ -278,7 +278,7 @@ class SegmentioCell: UICollectionViewCell {
segmentImageViewTopConstraint
])
}


fileprivate func setupContent(content: SegmentioItem) {
if style.isWithImage() {
Expand All @@ -295,6 +295,8 @@ class SegmentioCell: UICollectionViewCell {
segmentTitleLabel?.text = content.title
segmentTitleLabel?.minimumScaleFactor = 0.5
segmentTitleLabel?.adjustsFontSizeToFitWidth = true
guard let text = segmentTitleLabel?.text else { return }
segmentTitleLabel?.attributedText = NSAttributedString(string: text, attributes: options.attributedKey)
}
}

Expand All @@ -306,7 +308,7 @@ class SegmentioCell: UICollectionViewCell {
bottomConstraint?.constant = padding + indicatorOptions.height
}
}

// MARK: - Vertical separator

fileprivate func addVerticalSeparator() {
Expand Down
18 changes: 11 additions & 7 deletions Segmentio/Source/SegmentioOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public struct SegmentioItem {
label.sizeToFit()
return label.intrinsicContentSize.width
}

public init(title: String?, image: UIImage?, selectedImage: UIImage? = nil) {
self.title = title
self.image = image
Expand Down Expand Up @@ -98,7 +98,7 @@ public struct SegmentioVerticalSeparatorOptions {
self.ratio = ratio
self.color = color
}

}

// MARK: - Indicator
Expand Down Expand Up @@ -166,7 +166,7 @@ public enum SegmentioStyle: String {
return false
}
}

public var layoutMargins: CGFloat {
let defaultLayoutMargins: CGFloat = 8.0
switch self {
Expand Down Expand Up @@ -194,6 +194,7 @@ public struct SegmentioOptions {
var labelTextNumberOfLines: Int
var states: SegmentioStates
var animationDuration: CFTimeInterval
var attributedKey: [NSAttributedString.Key : Any]

public init() {
self.backgroundColor = .lightGray
Expand All @@ -206,11 +207,12 @@ public struct SegmentioOptions {
self.labelTextAlignment = .center
self.labelTextNumberOfLines = 0
self.states = SegmentioStates(defaultState: SegmentioState(),
selectedState: SegmentioState(),
highlightedState: SegmentioState())
selectedState: SegmentioState(),
highlightedState: SegmentioState())
self.animationDuration = 0.1
self.attributedKey = [:]
}

public init(backgroundColor: UIColor = .lightGray,
segmentPosition: SegmentioPosition = .fixed(maxVisibleItems: 4),
scrollEnabled: Bool = true,
Expand All @@ -223,7 +225,8 @@ public struct SegmentioOptions {
segmentStates: SegmentioStates = SegmentioStates(defaultState: SegmentioState(),
selectedState: SegmentioState(),
highlightedState: SegmentioState()),
animationDuration: CFTimeInterval = 0.1) {
animationDuration: CFTimeInterval = 0.1,
attributedKey: [NSAttributedString.Key : Any] = [:]) {
self.backgroundColor = backgroundColor
self.segmentPosition = segmentPosition
self.scrollEnabled = scrollEnabled
Expand All @@ -235,5 +238,6 @@ public struct SegmentioOptions {
self.labelTextNumberOfLines = labelTextNumberOfLines
self.states = segmentStates
self.animationDuration = animationDuration
self.attributedKey = attributedKey
}
}