From 361cffc516b5e13d0f470265d26f8030d8ec634d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yus=CC=A7a=20Dog=CC=86ru?= Date: Thu, 6 Dec 2018 18:21:32 +0300 Subject: [PATCH] Added NSAttributedString to the SegmentioItems --- Segmentio/Source/Cells/SegmentioCell.swift | 16 +++++++++------- Segmentio/Source/SegmentioOptions.swift | 18 +++++++++++------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/Segmentio/Source/Cells/SegmentioCell.swift b/Segmentio/Source/Cells/SegmentioCell.swift index e914ca3..3a8c15d 100644 --- a/Segmentio/Source/Cells/SegmentioCell.swift +++ b/Segmentio/Source/Cells/SegmentioCell.swift @@ -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) @@ -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 @@ -144,7 +144,7 @@ class SegmentioCell: UICollectionViewCell { segmentTitleLabel?.minimumScaleFactor = 0.5 segmentTitleLabel?.adjustsFontSizeToFitWidth = true } - + if (style != .onlyLabel) { segmentImageView?.image = selected ? selectedImage : image } @@ -177,7 +177,7 @@ class SegmentioCell: UICollectionViewCell { setupImageContainerConstraints() return // implement in subclasses } - + // MARK: - Private functions fileprivate func setupContainerConstraints() { @@ -217,7 +217,7 @@ class SegmentioCell: UICollectionViewCell { segmentTitleLabelTrailingConstraint, segmentTitleLabelVerticalCenterConstraint, segmentTitleLabelLeadingConstraint - ]) + ]) } fileprivate func setupImageContainerConstraints() { @@ -278,7 +278,7 @@ class SegmentioCell: UICollectionViewCell { segmentImageViewTopConstraint ]) } - + fileprivate func setupContent(content: SegmentioItem) { if style.isWithImage() { @@ -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) } } @@ -306,7 +308,7 @@ class SegmentioCell: UICollectionViewCell { bottomConstraint?.constant = padding + indicatorOptions.height } } - + // MARK: - Vertical separator fileprivate func addVerticalSeparator() { diff --git a/Segmentio/Source/SegmentioOptions.swift b/Segmentio/Source/SegmentioOptions.swift index f6f5e12..bc13073 100644 --- a/Segmentio/Source/SegmentioOptions.swift +++ b/Segmentio/Source/SegmentioOptions.swift @@ -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 @@ -98,7 +98,7 @@ public struct SegmentioVerticalSeparatorOptions { self.ratio = ratio self.color = color } - + } // MARK: - Indicator @@ -166,7 +166,7 @@ public enum SegmentioStyle: String { return false } } - + public var layoutMargins: CGFloat { let defaultLayoutMargins: CGFloat = 8.0 switch self { @@ -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 @@ -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, @@ -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 @@ -235,5 +238,6 @@ public struct SegmentioOptions { self.labelTextNumberOfLines = labelTextNumberOfLines self.states = segmentStates self.animationDuration = animationDuration + self.attributedKey = attributedKey } }